10 Examples of PHP code that must be collected

Source: Internet
Author: User
Tags foreach array exit explode header sql php code split

One, blacklist filtration

 
 
  1. function Is_spam ($text, $file, $split = ': ', $regex = False) {
  2. $handle = fopen ($file, ' RB ');
  3. $contents = Fread ($handle, FileSize ($file));
  4. Fclose ($handle);
  5. $lines = Explode ("n", $contents);
  6. $arr = Array ();
  7. foreach ($lines as $line) {
  8. List ($word, $count) = Explode ($split, $line);
  9. if ($regex)
  10. $arr [$word] = $count;
  11. Else
  12. $arr [Preg_quote ($word)] = $count;
  13. }
  14. Preg_match_all ("~". Implode (", Array_keys ($arr))." ~ ", $text, $matches);
  15. $temp = Array ();
  16. foreach ($matches [0] as $match) {
  17. if (!in_array ($match, $temp)) {
  18. $temp [$match] = $temp [$match] + 1;
  19. if ($temp [$match] >= $arr [$word])
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. $file = ' spam.txt ';
  26. $str = ' This string has cat, dog word ';
  27. if (Is_spam ($str, $file))
  28. Echo ' This is spam ';
  29. Else
  30. Echo ' is not spam ';
  31. Ab:3
  32. Dog:3
  33. Cat:2
  34. Monkey:2

Second, random color generator

 
 
  1. function Randomcolor () {
  2. $str = ' # ';
  3. for ($i = 0; $i < 6; $i + +) {
  4. $randNum = rand (0, 15);
  5. Switch ($randNum) {
  6. Case: $randNum = ' A '; Break
  7. Case one: $randNum = ' B '; Break
  8. Case: $randNum = ' C '; Break
  9. Case: $randNum = ' D '; Break
  10. Case: $randNum = ' E '; Break
  11. Case: $randNum = ' F '; Break
  12. }
  13. $str. = $randNum;
  14. }
  15. return $str;
  16. }
  17. $color = Randomcolor ();

Third, download files from the Internet

 
 
  1. Set_time_limit (0);
  2. Supports all file types
  3. URL here:
  4. $url = ' http://somsite.com/some_video.flv ';
  5. $pi = PathInfo ($url);
  6. $ext = $pi [' extension '];
  7. $name = $pi [' filename '];
  8. Create a new CURL resource
  9. $ch = Curl_init ();
  10. Set URL and other appropriate options
  11. curl_setopt ($ch, Curlopt_url, $url);
  12. curl_setopt ($ch, Curlopt_header, false);
  13. curl_setopt ($ch, Curlopt_binarytransfer, true);
  14. curl_setopt ($ch, Curlopt_autoreferer, true);
  15. curl_setopt ($ch, curlopt_followlocation, true);
  16. curl_setopt ($ch, Curlopt_returntransfer, true);
  17. Grab URL and pass it to the browser
  18. $opt = curl_exec ($ch);
  19. Close CURL Resource.
  20. Curl_close ($ch);
  21. $saveFile = $name. $ext;
  22. if (Preg_match ("/[^0-9a-z._-]/i", $saveFile))
  23. $saveFile = MD5 (Microtime (TRUE)). $ext;
  24. $handle = fopen ($saveFile, ' WB ');
  25. Fwrite ($handle, $opt);
  26. Fclose ($handle);

Four, Alexa/google Page Rank

 
 
  1. function Page_rank ($page, $type = ' Alexa ') {
  2. Switch ($type) {
  3. Case ' Alexa ':
  4. $url = ' http://alexa.com/siteinfo/';
  5. $handle = fopen ($url. $page, ' R ');
  6. Break
  7. Case ' Google ':
  8. $url = ' http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info: ';
  9. $handle = fopen ($url. ' http://'. $page, ' R ');
  10. Break
  11. }
  12. $content = Stream_get_contents ($handle);
  13. Fclose ($handle);
  14. $content = Preg_replace ("~ (ntss+) ~", ", $content);
  15. Switch ($type) {
  16. Case ' Alexa ':
  17. if (Preg_match (' ~<div class= data (downup) > (. +?) </div>~im ', $content, $matches)) {
  18. return $matches [2];
  19. }else{
  20. return FALSE;
  21. }
  22. Break
  23. Case ' Google ':
  24. $rank = Explode (': ', $content);
  25. if ($rank [2]!= ')
  26. return $rank [2];
  27. Else
  28. return FALSE;
  29. Break
  30. Default
  31. return FALSE;
  32. Break
  33. }
  34. }
  35. Alexa Page Rank:
  36. Echo ' Alexa Rank: '. Page_rank (' techug.com ');
  37. Echo ';
  38. Google Page Rank
  39. Echo ' Google Rank: '. Page_rank (' techug.com ', ' Google ');

V. Force download files

 
 
  1. $filename = $_get[' file ']; Get the Fileid from the URL
  2. Query the file ID
  3. $query = sprintf ("SELECT * FROM tablename WHERE id = '%s '", Mysql_real_escape_string ($filename));
  4. $sql = mysql_query ($query);
  5. if (mysql_num_rows ($sql) > 0) {
  6. $row = Mysql_fetch_array ($sql);
  7. Set some headers
  8. Header ("Pragma:public");
  9. Header ("expires:0");
  10. Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
  11. Header ("Content-type:application/force-download");
  12. Header ("Content-type:application/octet-stream");
  13. Header ("Content-type:application/download");
  14. Header ("content-disposition:attachment; Filename= ". basename ($row [' FileName ']).";
  15. Header ("Content-transfer-encoding:binary");
  16. Header ("Content-length:" FileSize ($row [' FileName ']));
  17. @readfile ($row [' FileName ']);
  18. Exit (0);
  19. }else{
  20. Header ("Location:/");
  21. Exit
  22. }

VI, using email to display the user's gravator avatar

 
  
  
  1. $gravatar _link = ' http://www.gravatar.com/avatar/'. MD5 ($comment _author_email). ' S=32 ';
  2. Echo ' ';

Seven, use curl to get the number of RSS subscriptions

 
 
  1. $ch = Curl_init ();
  2. curl_setopt ($ch, Curlopt_url, ' https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=7qkrmib4r9rscbplq5qgadiiq4 ');
  3. curl_setopt ($ch, curlopt_returntransfer,1);
  4. curl_setopt ($ch, curlopt_connecttimeout,2);
  5. $content = curl_exec ($ch);
  6. $subscribers = Get_match ('/circulation= ' (. *) "/isu", $content);
  7. Curl_close ($ch);
  8. Viii. Time Difference Calculation
  9. Function ago ($time)
  10. {
  11. $periods = Array ("Second", "Minute", "Hour", "Day", "Week", "Month", "year", "decade");
  12. $lengths = Array ("60", "60", "24", "7", "4.35", "12", "10");
  13. $now = time ();
  14. $difference = $now-$time;
  15. $tense = "Ago";
  16. for ($j = 0; $difference >= $lengths [$j] && $j < count ($lengths)-1; $j + +) {
  17. $difference/= $lengths [$j];
  18. }
  19. $difference = round ($difference);
  20. if ($difference!= 1) {
  21. $periods [$j].= "s";
  22. }
  23. Return "$difference $periods [$j] ' ago '";
  24. }

Ix. Interception of pictures

 
 
  1. $filename = "Test.jpg";
  2. List ($w, $h, $type, $attr) = getimagesize ($filename);
  3. $src _im = Imagecreatefromjpeg ($filename);
  4. $src _x = ' 0 '; Begin X
  5. $src _y = ' 0 '; Begin Y
  6. $src _w = ' 100 '; Width
  7. $src _h = ' 100 '; Height
  8. $DST _x = ' 0 '; Destination X
  9. $dst _y = ' 0 '; Destination y
  10. $DST _im = Imagecreatetruecolor ($src _w, $src _h);
  11. $white = Imagecolorallocate ($dst _im, 255, 255, 255);
  12. Imagefill ($dst _im, 0, 0, $white);
  13. Imagecopy ($dst _im, $src _im, $dst _x, $dst _y, $src _x, $src _y, $src _w, $src _h);
  14. Header ("Content-type:image/png");
  15. Imagepng ($dst _im);
  16. Imagedestroy ($dst _im);

Ten, check whether the website is down

 
 
  1. function Visit ($url) {
  2. $agent = "mozilla/4.0" (compatible; MSIE 5.01; Windows NT 5.0) "; $ch =curl_init ();
  3. curl_setopt ($ch, Curlopt_url, $url);
  4. curl_setopt ($ch, curlopt_useragent, $agent);
  5. curl_setopt ($ch, Curlopt_returntransfer, 1);
  6. curl_setopt ($ch, Curlopt_verbose,false);
  7. curl_setopt ($ch, Curlopt_timeout, 5);
  8. curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
  9. curl_setopt ($ch, curlopt_sslversion,3);
  10. curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);
  11. $page =curl_exec ($ch);
  12. echo Curl_error ($ch);
  13. $httpcode = Curl_getinfo ($ch, Curlinfo_http_code);
  14. Curl_close ($ch);
  15. if ($httpcode >=200 && $httpcode <300) return true;
  16. else return false;
  17. }
  18. if (Visit ("http://www.google.com"))
  19. echo "Website OK". n ";
  20. Else
  21. echo "Website down";
"Responsible Editor: Wangxueyan TEL: (010) 68476606"


Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.