10 Super Useful PHP code clips decisive collection _php tips

Source: Internet
Author: User

This small series will give you 10 of the most useful PHP code snippets.

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.917404); 
$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.6025 feet:13,741.4350 yards:4,580.4783 kilometers:4.1884 meters :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. Detect geographical location 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, CURLO Pt_useragent => $curlopt _useragent, Curlopt_url => $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/provi NCE: ([^<]*)}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 " 
"; 

6. Detect browser language, only available $availablelanguages as 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 the user wants for 
 ($i = $times; $i >0; $i-) { 
 //Encode with BAS 
 E64 ... $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, the return of the value return 
 $hash; 

10a. The tweeter Feed runner--uses any Twitter name to load user resources on any page.

 pversion; The 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; 
 The 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; 
 The Public Function Gettimelinearray () {return simplexml_load_string ($this->xml); The 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 themes, such as: example.php

Loadtimeline ("Phpsnips")->gettweets (); 
foreach ($feed as $time => $message) { 
 echo ' <div class= ' tweet ' > '. $twitter->formattweet ($message). " <BR/>at: ". $time." </div> "; 
} 

Directly to use, 10 pieces of PHP code, but also hesitate what, decisively collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.