6 Ultra-Practical PHP code Snippets _php Instances

Source: Internet
Author: User
Tags learn php programming
First, blacklist filter

function Is_spam ($text, $file, $split = ': ', $regex = False) {   $handle = fopen ($file, ' RB ');   $contents = Fread ($handle, FileSize ($file));   Fclose ($handle);   $lines = Explode ("n", $contents); $arr = Array (); foreach ($lines as $line) {list ($word, $count) = Explode ($split, $line), if ($regex) $arr [$word] = $count; else $arr [Preg_qu OTE ($word)] = $count; } preg_match_all ("~". Implode (' | ', Array_keys ($arr)). " ~ ", $text, $matches); $temp = Array (); foreach ($matches [0] as $match) {if (!in_array ($match, $temp)) {$temp [$match] = $temp [$match] + 1; if ($temp [$match] >= $a rr[$word]) return true; }} return false; }  $file = ' spam.txt '; $str = ' This string have cat, dog word '; if (Is_spam ($str, $file)) echo ' This is spam '; else echo ' This isn't spam ';  

Second, random color generator

function Randomcolor () {   $str = ' # ';   for ($i = 0; $i < 6; $i + +) {     $randNum = rand (0);     Switch ($randNum) {case       ten: $randNum = ' A ';       Case one: $randNum = ' B '; break;       Case: $randNum = ' C '; break;       Case: $randNum = ' D '; break;       Case: $randNum = ' E '; break;       Case: $randNum = ' F '; break;     }     $str. = $randNum;   }   

Third, download the file from the Internet

Set_time_limit (0); Supports all file types//urls here: $url = ' http://somsite.com/some_video.flv '; $pi = PathInfo ($url); $ext = $pi [' extension ']; $name = $pi [' filename '];  Create a new CURL resource $ch = Curl_init ();  Set URL and other appropriate options curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_header, false); curl_setopt ($ch, Curlopt_binarytransfer, true); curl_setopt ($ch, Curlopt_autoreferer, true); curl_setopt ($ch, curlopt_followlocation, true); curl_setopt ($ch, Curlopt_returntransfer, true);  Grab URL and pass it to the browser $opt = curl_exec ($ch);  Close CURL resource, and free up system resources curl_close ($ch);  $saveFile = $name. '. '. $ext; if (Preg_match ("/[^0-9a-z._-]/i", $saveFile)) $saveFile = MD5 (Microtime (TRUE)). $ext;  

Iv. Mandatory download of files

$filename = $_get[' file ']; Get the Fileid from the URL//Query of the file id $query = sprintf ("select * from tableName WHERE ID = '%s '", mysql_real_e Scape_string ($filename)); $sql = mysql_query ($query); if (mysql_num_rows ($sql) > 0) {$row = Mysql_fetch_array ($sql);//Set Some headers header ("Pragma:public"); Header ("Exp Ires:0 "); Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0"); Header ("Content-type:application/force-download"); Header ("Content-type:application/octet-stream"); Header ("Content-type:application/download"); Header ("content-disposition:attachment; Filename= ". basename ($row [' FileName '])."; "); Header ("Content-transfer-encoding:binary"); Header ("Content-length:". FileSize ($row [' FileName ']);  

V. Interception of pictures

$filename = "Test.jpg"; List ($w, $h, $type, $attr) = getimagesize ($filename); $src _im = Imagecreatefromjpeg ($filename);  $src _x = ' 0 '; Begin x $src _y = ' 0 '; Begin y $src _w = ' 100 '; Width $src _h = ' 100 '; Height $dst _x = ' 0 '; Destination x $dst _y = ' 0 ';  Destination y  $dst _im = Imagecreatetruecolor ($src _w, $src _h); $white = Imagecolorallocate ($dst _im, 255, 255, 255); Imagefill ($dst _im, 0, 0, $white);  Imagecopy ($dst _im, $src _im, $dst _x, $dst _y, $src _x, $src _y, $src _w, $src _h);  

Vi. checking the website for downtime

function Visit ($url) {     

The above is 6 super-practical PHP code sample, I hope that you learn PHP programming Help, a decisive collection of it

  • Related Article

    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.