PHP is an HTML-embedded language that is a scripting language for embedded HTML documents executed on the server side. PHP has hundreds of basic features that support thousands of extensions. These features are well loaded on the PHP site, but the built-in libraries have a variety of names. The PHP code base contains countless useful snippets of PHP code, and each developer needs to constantly refine his "toolbox". With these snippets you can save a lot of time and take a look.
1. Find the distance between longitudes and latitudes
function Getdistancebetweenpointsnew ($latitude 1, $longitude 1, $latitude 2, $longitude 2) {$theta = $longitude 1-$longit Ude2; $miles = (sin (Deg2rad ($latitude 1)) * Sin (Deg2rad ($latitude 2)) + (cos (Deg2rad ($latitude 1)) * cos (Deg2rad ($latitude 2)) * Cos (Deg2rad ($theta))); $miles = ACOs ($miles); $miles = Rad2deg ($miles); $miles = $miles * 60 * 1.1515; $feet = $miles * 5280; $yards = $feet/3; $kilometers = $miles * 1.609344; $meters = $kilometers * 1000; Return compact (' Miles ', ' feet ', ' yards ', ' kilometers ', ' meters '); $point 1 = Array (' lat ' = = 40.770623, ' long ' = -73.964367); $point 2 = Array (' lat ' = + 40.758224, ' long ' = 73.91 7404); $distance = getdistancebetweenpointsnew ($point 1[' lat '), $point 1[' long ', $point 2[' lat '], $point 2[' long ']); foreach ($distance as $unit = + $value) {echo $unit. ': '. Number_format ($value, 4). ';} The example returns the following:miles:2.6025feet:13,741.4350yards:4,580.4783kilometers:4.1884meters:4,188.3894
2. Perfect Curl function
function Xcurl ($url, $ref =null, $post =array (), $ua = "mozilla/5.0 (X11; Linux x86_64; Rv:2.2a1pre) gecko/20110324 firefox/4.2a1pre ", $print =false) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_autoreferer, true); if (!empty ($ref)) { curl_setopt ($ch, Curlopt_referer, $ref); } curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_returntransfer, 1); if (!empty ($ua)) { curl_setopt ($ch, curlopt_useragent, $ua); } if (count ($post) > 0) { curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, $post); } $output = curl_exec ($ch); Curl_close ($ch); if ($print) { print ($output); } else { return $output;} }
3. Clean up user input
]*?>.*? @si ', //Strip out JavaScript ' @<[\/\!] *? [^<>]*?> @si ', //Strip out HTML tags ' @]*?>.*? @siU ', //Strip style tags properly ' @@ ' //Strip multi-line comments ); $output = Preg_replace ($search, ", $input); return $output; }? > $val) { $output [$var] = sanitize ($val); } } else { if (GET_MAGIC_QUOTES_GPC ()) { $input = stripslashes ($input); } $input = CleanInput ($input); $output = mysql_real_escape_string ($input); } return $output;}? >
4. Location detection via IP (city, country)
function Detect_city ($ip) {$default = ' Hollywood, CA '; if (!is_string ($IP) | | strlen ($IP) < 1 | | $ip = = ' 127.0.0.1 ' | | $ip = = ' localhost ') $ip = ' 8.8.8.8 ' ; $curlopt _useragent = ' mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.9.2) gecko/20100115 firefox/3.6 (. NET CLR 3.5.30729) '; $url = ' http://ipinfodb.com/ip_locator.php?ip= '. UrlEncode ($IP); $ch = Curl_init (); $curl _opt = Array (curlopt_followlocation = 1, Curlopt_header = 0, Curlopt_returntransfer = 1, curlopt_useragent = $curlopt _useragent, Curlopt_url =& Gt $url, curlopt_timeout = 1, curlopt_referer = ' http:/'. $_server[' Http_host '],); Curl_setopt_array ($ch, $curl _opt); $content = curl_exec ($ch); if (!is_null ($curl _info)) {$curl _info = Curl_getinfo ($ch); } curl_close ($ch); if (Preg_match (' {City: ([^<]*)}i ', $content, $regs)) {$city = $regs [1];} if (Preg_match (' {State/provin CE: ([^<]*)}i ', $content, $regs)) {$state = $regs [1];} if ($city! = "&& $state! =") {$location = $city. ', ' . $state; return $location; }else{return $default;}}
5. Set Password strength
{ $strength =; } return $strength;} Var_dump (Password_strength ("Correct Horse Battery staple"); echo ""; Var_dump (Password_strength ("Super Monkey Ball")) echo ""; Var_dump (Password_strength ("tr0ub4dor&3"); echo ""; Var_dump (Password_strength ("abc123"); echo ""; var _dump (Password_strength ("Sweet"));
6. Detection of browser language, only available $availablelanguages as an array (' en ', ' de ', ' es ')
function Get_client_language ($availableLanguages, $default = ' en ') { if (isset ($_server[' http_accept_language ')) { $langs =explode (', ', $_server[' http_accept_language ')); Start going through each one foreach ($langs as $value) { $choice =substr ($value, 0,2); if (In_array ($choice, $availableLanguages)) { return $choice; } } } return $default;}
7. Create a Data URL
function Data_uri ($file, $mime) { $contents =file_get_contents ($file); $base 64=base64_encode ($contents); echo "Data: $mime; base64, $base 64";}
8. Create a more friendly page title SEO URL
Input example: $title = "This foo's Bar is Rockin ' cool!"; echo Makeseoname ($title); RETURNS://this-foos-bar-is-rockin-cool
function Make_seo_name ($title) { return preg_replace ('/[^a-z0-9_-]/i ', ' ', Strtolower (' str_replace ', '-', trim ($ (title))));}
9. Ultimate Encryption function
F (ucking) U (ncrackable) e (ncryption) function by Blackhatdbl (www.netforme.net) function Fue ($hash, $times) { // Execute the encryption (s) as many times as the user wants for ($i = $times; $i >0; $i-) { //Encode with base64...
$hash =base64_encode ($hash); and MD5 ... $hash =md5 ($hash); SHA1 ... $hash =SHA1 ($hash); Sha256 ... (One more) $hash =hash ("sha256", $hash); sha512 $hash =hash ("sha512", $hash); } Finaly, when done, return the value return $hash;}
10a. Tweeter Feed runner--Use any Twitter name to load user resources on any page.
Pversion; Public Function Loadtimeline ($user, $max =) {$this->twiturl. = ' Statuses/user_timeline.xml?screen_name= ' . $user. ' &count= '. $max; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $this->twiturl); curl_setopt ($ch, Curlopt_returntransfer, true); $this->xml = curl_exec ($ch); return $this; Public Function Gettweets () {$this->twitterarr = $this->gettimelinearray (); $tweets = Array (); foreach ($this->twitterarr->status as $status) {$tweets [$status->created_at->__tostring ()] = $status ->text->__tostring (); } return $tweets; } public Function Gettimelinearray () {return simplexml_load_string ($this->xml); Public Function Formattweet ($tweet) {$tweet = Preg_replace ("/(http (. +?)) (|$)/"," $1$3 ", $tweet); $tweet = Preg_replace ("/# (. +?) (\h|\w|$)/"," #$1$2 ", $tweet); $tweet = Preg_replace ("/@ (. +?) (\h|\w|$)/"," @$1$2 ", $tweet); return $tweet; }}
10b. Tweeter Feed runner--is used to create files in the theme, such as: example.php
Loadtimeline ("Phpsnips")->gettweets (), foreach ($feed as $time + $message) { echo "<div class= ' tweet ' > ". $twitter->formattweet ($message)." <BR/>at: ". $time." </div> ";}