Pentiums Posted June 24, 2010 Author Report Posted June 24, 2010 (edited) function randString($length) { $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; $string = ''; for ($p = 0; $p < $length; $p++) { $string .= $chars[mt_rand(0, strlen($chars))]; } return $string; } Edited June 24, 2010 by Pentiums Quote
rATRIJS Posted June 24, 2010 Report Posted June 24, 2010 (edited) Katru reizi saukt strlen loop'aa nav forshi :( + buus FAIL, ja rand izmetiis tieshi to strlen, jo nav, taada indeksa masiivaa (stringaa)... Edited June 24, 2010 by rATRIJS Quote
codez Posted June 24, 2010 Report Posted June 24, 2010 (edited) Šeit vēl 2 random stringa funkcijas varianti, īsāk nevarēju izdomāt: function randString($l) { for($s='',$i=0; $i<$l; $i++) { $s.=chr(rand(1,36)>10?rand(97,122):rand(48,57)); } return $s; } function rs($l) { return $l==0?"":rs($l-1).chr(rand(1,36)>10?rand(97,122):rand(48,57)); } EDIT: tiekšanās uz īsumu: function rs1($l) { for($s='',$i=0; $i<$l; $i++,$s.=chr(rand(1,36)>10?rand(97,122):rand(48,57))); return $s; } function rs2($l) { for($s=''; strlen($s)<$l; $s.=chr(rand(1,36)>10?rand(97,122):rand(48,57))); return $s; } function rs3($l) { for($s='';$l>0;$l--,$s.=chr(rand(1,36)>10?rand(97,122):rand(48,57))); return $s; } EDIT: vēl īsāk: function rs4($l) { for($s='';$l>0;$l--,$s.=rand(0,35)>9?chr(rand(97,122)):rand(0,9)); return $s; } function rs5($l) { for($s='';$l;$l--,$s.=($a=rand(0,35))>9?chr($a+87):$a); return $s; } function rs6($l) { return $l?rs6($l-1).(($a=rand(0,35))>9?chr($a+87):$a):""; } Edited June 24, 2010 by codez Quote
daGrevis Posted June 25, 2010 Report Posted June 25, 2010 (edited) Mans variants. =) function get__random_characters($length, $type) { // By daGrevis. // Version 1.1. switch($type) { case 'alphabet': $characters = 'abcdefghijklmnopqrstuvwqyz' . 'ABCDEFGHIJKLMNOPQRSTUVWQYZ'; break; case 'alphabet__uppercase': $characters = 'ABCDEFGHIJKLMNOPQRSTUVWQYZ'; break; case 'alphabet__lowercase': $characters = 'abcdefghijklmnopqrstuvwqyz'; break; case 'numeric': $characters = '0123456789'; break; case 'alpha_numeric': $characters = 'abcdefghijklmnopqrstuvwqyz' . 'ABCDEFGHIJKLMNOPQRSTUVWQYZ' . '0123456789'; break; case 'alpha_numeric__uppercase': $characters = 'ABCDEFGHIJKLMNOPQRSTUVWQYZ' . '0123456789'; break; case 'alpha_numeric__lowercase': $characters = 'abcdefghijklmnopqrstuvwqyz' . '0123456789'; break; } $string = ''; for($i = 0; $length > $i; $i++) { $string .= $characters{mt_rand(0, strLen($characters) - 1)}; } return $string; } Edited June 25, 2010 by daGrevis Quote
marrtins Posted June 26, 2010 Report Posted June 26, 2010 function randString($length) { if($length <= 0) return ''; return substr(md5(uniqid("", true)), 0, $length).randString($length - 32); } Quote
briedis Posted August 10, 2010 Report Posted August 10, 2010 (edited) Drusku jāatdzīvina topiks :) Funkcija atgriež drošu faila vārdu, paredzēts lietošanai kopā ar funkciju move_uploaded_file(). /** * Atgriež tīmekļa-drošu faila nosaukumu * @param string $filename Faila nosaukums * @param string $default Noklusētais faila nosaukums kļūdas gadījumā * @return string Tīmekļa-drošs faila nosaukums */ function getSafeFilename($filename, $default = 'file'){ $filename = trim(preg_replace('/[^a-z\d.]+/i', '_', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $filename)),'_'); return $filename===''||$filename==='_'?$default:$filename; } EDIT: ņemti vērā indoom ieteikumi! Paldies, indoom! Edited August 11, 2010 by briedis Quote
indoom Posted August 10, 2010 Report Posted August 10, 2010 (edited) nemaz tik safe nav. Krievu (kirilica) burtus nesapratīs. Rādīs iconv notici "Notice: iconv() [function.iconv]: Detected an illegal character in input string" Tad labāk kaut kā šitā function getSafeFilename($filename, $default = 'file'){ $filename = trim(preg_replace('/[^a-z\d.]+/i', '_', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $filename)),'_'); return $filename===''||$filename==='_'?$default:$filename; } Edited August 10, 2010 by indoom Quote
Klez Posted August 10, 2010 Report Posted August 10, 2010 faila drosai glabasanai iesaku izmantot sitaa: $name = md5(microtime() . $_FILES['userfile']['name']) . '.' . substr(strrchr($_FILES['userfile']['name'], '.'), 1); //nav ne jausmas kas notiks ja pievienos failu bez paplasinaajuma, bet tas ir cits staats :) move_uploaded_file($_FILES['userfile']['tmp_name'],$name); //iex db saglabaa nogenereeto faila nos, orginaalo faila nos, ko peec tam ar hederiem padod pie failu download Quote
Pentiums Posted September 3, 2010 Author Report Posted September 3, 2010 Paroles ģenerators ar iespēju mainīt stiprumu un garumu function generatePassword($length=9, $strength=0) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength & 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; } if ($strength & 2) { $vowels .= "AEUY"; } if ($strength & 4) { $consonants .= '23456789'; } if ($strength & 8) { $consonants .= '@#$%'; } $password = ''; $alt = time() % 2; for ($i = 0; $i < $length; $i++) { if ($alt == 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password; } Quote
marrtins Posted September 3, 2010 Report Posted September 3, 2010 Topika atdzīvināšanai no manis arī pārītis. Konvertē objekta propertijus, masīvus un parastu variabli no utf8 uz win-1257 un otrādi. function utf2win($data) { if(is_array($data)) { foreach($data as $k=>$v) $data[$k] = utf2win($v); } elseif(is_object($data)) { $a = get_object_vars($data); foreach($a as $k=>$v) $data->{$k} = utf2win($v); } else { $data = mb_convert_encoding($data, 'ISO-8859-13', 'UTF-8'); } return $data; } // utf2win function win2utf($data) { if(is_array($data)) { foreach($data as $k=>$v) $data[$k] = win2utf($v); } elseif(is_object($data)) { $a = get_object_vars($data); foreach($a as $k=>$v) $data->{$k} = win2utf($v); } else { $data = mb_convert_encoding($data, 'UTF-8', 'ISO-8859-13'); } return $data; } // win2utf Quote
darksign Posted December 20, 2010 Report Posted December 20, 2010 Pārvērš naudu vārdos http://php.lv/f/topic/4064-skaitlu-parveidotajs-par-tekstu/page__view__findpost__p__139763 Quote
briedis Posted January 2, 2011 Report Posted January 2, 2011 Funkcija pārbauda, vai lietotājs ir bots (spriežot pēc tā user-agent'a). Noderīgi, ja vajag veidot hit'u statistiku, kur bot'a apmeklējums neskaitās. /** * Pārbaudam, vai lietotājs ir bots. (spriežot pec user_agent) * @param string $user_agent HTTP_USER_AGENT * @return bool */ function isBot($user_agent){ $bots = array('bing', 'msn', 'abacho', 'abcdatos', 'abcsearch', 'acoon', 'adsarobot', 'aesop', 'ah-ha', 'alkalinebot', 'almaden', 'altavista', 'antibot', 'anzwerscrawl', 'aol', 'search', 'appie', 'arachnoidea', 'araneo', 'architext', 'ariadne', 'arianna', 'ask', 'jeeves', 'aspseek', 'asterias', 'astraspider', 'atomz', 'augurfind', 'backrub', 'baiduspider', 'bannana_bot', 'bbot', 'bdcindexer', 'blindekuh', 'boitho', 'boito', 'borg-bot', 'bsdseek', 'christcrawler', 'computer_and_automation_research_institute_crawler', 'coolbot', 'cosmos', 'crawler', 'crawler@fast', 'crawlerboy', 'cruiser', 'cusco', 'cyveillance', 'deepindex', 'denmex', 'dittospyder', 'docomo', 'dogpile', 'dtsearch', 'elfinbot', 'entire', 'web', 'esismartspider', 'exalead', 'excite', 'ezresult', 'fast', 'fast-webcrawler', 'fdse', 'felix', 'fido', 'findwhat', 'finnish', 'firefly', 'firstgov', 'fluffy', 'freecrawl', 'frooglebot', 'galaxy', 'gaisbot', 'geckobot', 'gencrawler', 'geobot', 'gigabot', 'girafa', 'goclick', 'goliat', 'googlebot', 'griffon', 'gromit', 'grub-client', 'gulliver', 'gulper', 'henrythemiragorobot', 'hometown', 'hotbot', 'htdig', 'hubater', 'ia_archiver', 'ibm_planetwide', 'iitrovatore-setaccio', 'incywincy', 'incrawler', 'indy', 'infonavirobot', 'infoseek', 'ingrid', 'inspectorwww', 'intelliseek', 'internetseer', 'ip3000.com-crawler', 'iron33', 'jcrawler', 'jeeves', 'jubii', 'kanoodle', 'kapito', 'kit_fireball', 'kit-fireball', 'ko_yappo_robot', 'kototoi', 'lachesis', 'larbin', 'legs', 'linkwalker', 'lnspiderguy', 'look.com', 'lycos', 'mantraagent', 'markwatch', 'maxbot', 'mercator', 'merzscope', 'meshexplorer', 'metacrawler', 'mirago', 'mnogosearch', 'moget', 'motor', 'muscatferret', 'nameprotect', 'nationaldirectory', 'naverrobot', 'nazilla', 'ncsa', 'beta', 'netnose', 'netresearchserver', 'ng/1.0', 'northerlights', 'npbot', 'nttdirectory_robot', 'nutchorg', 'nzexplorer', 'odp', 'openbot', 'openfind', 'osis-project', 'overture', 'perlcrawler', 'phpdig', 'pjspide', 'polybot', 'pompos', 'poppi', 'portalb', 'psbot', 'quepasacreep', 'rabot', 'raven', 'rhcs', 'robi', 'robocrawl', 'robozilla', 'roverbot', 'scooter', 'scrubby', 'search.ch', 'search.com.ua', 'searchfeed', 'searchspider', 'searchuk', 'seventwentyfour', 'sidewinder', 'sightquestbot', 'skymob', 'sleek', 'slider_search', 'slurp', 'solbot', 'speedfind', 'speedy', 'spida', 'spider_monkey', 'spiderku', 'stackrambler', 'steeler', 'suchbot', 'suchknecht.at-robot', 'suntek', 'szukacz', 'surferf3', 'surfnomore', 'surveybot', 'suzuran', 'synobot', 'tarantula', 'teomaagent', 'teradex', 't-h-u-n-d-e-r-s-t-o-n-e', 'tigersuche', 'topiclink', 'toutatis', 'tracerlock', 'turnitinbot', 'tutorgig', 'uaportal', 'uasearch.kiev.ua', 'uksearcher', 'ultraseek', 'unitek', 'vagabondo', 'verygoodsearch', 'vivisimo', 'voilabot', 'voyager', 'vscooter', 'w3index', 'w3c_validator', 'wapspider', 'wdg_validator', 'webcrawler', 'webmasterresourcesdirectory', 'webmoose', 'websearchbench', 'webspinne', 'whatuseek', 'whizbanglab', 'winona', 'wire', 'wotbox', 'wscbot', 'www.webwombat.com.au', 'xenu', 'link', 'sleuth', 'xyro', 'yahoobot', 'yahoo!', 'slurp', 'yandex', 'yellopet-spider', 'zao/0', 'zealbot', 'zippy', 'zyborg' ); $user_agent = strtolower($user_agent); foreach($bots as $bot){ if(strpos($user_agent, $bot) === true){ return true; } } return false; } Quote
briedis Posted February 9, 2011 Report Posted February 9, 2011 (edited) Atdzīvinam topiku :) Pēdējā laikā biju nonācis pie strupceļa, ka ir datubāze ar pilsētā, to atbilstošajām koordinātēm un laika zonas offset, bet, kā zinām, lai iegūtu precīzu laiku kādā pasaules vietā arī jāņem vērā DST (daylight savings time), kas atšķiras kā nu kur. Viens veids ir izmantot iebūvēto php funkcionalitāte, bet ir nepieciešams zināt laika zonas identifikatoru ("Europe/Riga" - mums, te), un samappot daudzas pasaules pilsētas ar to nav joka lieta, ņemot vērā, ka noskaidrot pilsētas laika zonu nav tik ātri veicams uzdevums (protams, daudzām valstīm ir tikai viena laika zona, kas atvieglo uzdevuma veikšanu) :) Piedāvāju kodu, kas to izdarīs jūsu vietā, izmantojot foršo Flickr API: /** * Funkcija iegūst laika zonas identifikatoru atbilstoši koordinātēm, izmantojot flickr API. * @param float $latitude Piemēram 24.342 * @param float $longitude Piemēram 56.756 * @return string Laika zonas identifikators, piem. "Europe/Riga" */ function getTimezoneIdentifierByCoordinates($latitude, $longitude){ $url = "http://api.flickr.com/services/rest/?method=flickr.places.findByLatLon"; $data = array( "api_key" => "", //Iegūstams: http://www.flickr.com/services/api/misc.api_keys.html "lat" => $latitude, "lon" => $longitude, ); //Veicam pieprasījumu $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL $xml = simplexml_load_string(curl_exec($ch)); //Atgriežam tikai laika zonas identifikatoru return (string)$xml->places->place['timezone']; } Moš kādam noderēs, man aiztaupīja pāris stundas manuāla darba, līdz uzzināju, ka Flickr piedāvā šādu api un uzcepu šo jauko funkciju :) Edited February 9, 2011 by briedis Quote
indoom Posted February 9, 2011 Report Posted February 9, 2011 kā var uzzināt lietotāja lat. un lon.? Quote
mad182 Posted February 9, 2011 Report Posted February 9, 2011 Man te mētājas f-ja, kas parāda jaunākos postus no twitter, ja nu kādam vajag :) http://exs.lv/mad182/twitter_feed.phps Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.