As a normal programmer, several languages are very normal, I believe that most programmers will also write a few PHP programs, if the Web programmer, PHP must be necessary, even if you do not use it to develop large software projects, but also how much understand its syntax.
Although PHP is often criticized, being belittled, being a joke, it turns out that PHP is the most widely used programming language in web development around the world. PHP's biggest drawback is too simple, grammar is not rigorous, the framework system is weak, but this is its biggest advantage, a bit of programming background of ordinary people, only need to learn PHP half-day time, you can start to develop Web applications.
Some of the features of several programming languages have been summed up on the internet, and I find it quite reasonable:
PHP is: Quick and Dirty
Java is: Beauty and slowly
Ruby is: Quick and Beauty
Python is: Quick and simple
In the popular popularity of PHP, the Internet summed up a lot of practical PHP code snippets, these snippets in when you encounter similar problems, paste the past can be used, very efficient, very time-saving. It is a good habit of a learning programmer to put the good code of these programmers ' predecessors into their knowledge base.
One, blacklist filtration
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_quote ($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] >= $arr [$word])
return true;
}
}
return false;
}
$file = ' spam.txt ';
$str = ' This string has cat, dog word ';
if (Is_spam ($str, $file))
Echo ' This is spam ';
Else
Echo ' is not spam ';
Ab:3
Dog:3
Cat:2
Monkey:2
Second, random color generator
function Randomcolor () {
$str = ' # ';
for ($i = 0; $i < 6; $i + +) {
$randNum = rand (0, 15);
Switch ($randNum) {
Case: $randNum = ' A '; Break
Case one: $randNum = ' B '; Break
Case: $randNum = ' C '; Break
Case: $randNum = ' D '; Break
Case: $randNum = ' E '; Break
Case: $randNum = ' F '; Break
}
$str. = $randNum;
}
return $str;
}
$color = Randomcolor ();
Third, download files from the network
Set_time_limit (0);
Supports all file types
URL 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.
Curl_close ($ch);
$saveFile = $name. $ext;
if (Preg_match ("/[^0-9a-z._-]/i", $saveFile))
$saveFile = MD5 (Microtime (TRUE)). $ext;
$handle = fopen ($saveFile, ' WB ');
Fwrite ($handle, $opt);
Fclose ($handle);
Four, Alexa/google Page Rank
function Page_rank ($page, $type = ' Alexa ') {
Switch ($type) {
Case ' Alexa ':
$url = ' http://alexa.com/siteinfo/';
$handle = fopen ($url. $page, ' R ');
Break
Case ' Google ':
$url = ' http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info: ';
$handle = fopen ($url. ' http://'. $page, ' R ');
Break
}
$content = Stream_get_contents ($handle);
Fclose ($handle);
$content = Preg_replace ("~ (n|t|ss+) ~", ", $content);
Switch ($type) {
Case ' Alexa ':
if (Preg_match (' ~<div class= data (down|up) > (. +?) </div>~im ', $content, $matches)) {
return $matches [2];
}else{
return FALSE;
}
Break
Case ' Google ':
$rank = Explode (': ', $content);
if ($rank [2]!= ')
return $rank [2];
Else
return FALSE;
Break
Default
return FALSE;
Break
}
}
Alexa Page Rank:
Echo ' Alexa Rank: '. Page_rank (' techug.com ');
Echo '
';
Google Page Rank
Echo ' Google Rank: '. Page_rank (' techug.com ', ' Google ');
V. Force download files
$filename = $_get[' file ']; Get the Fileid from the URL
Query the file ID
$query = sprintf ("SELECT * FROM tablename WHERE id = '%s '", Mysql_real_escape_string ($filename));
$sql = mysql_query ($query);
if (mysql_num_rows ($sql) > 0) {
$row = Mysql_fetch_array ($sql);
Set some headers
Header ("Pragma:public");
Header ("expires: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 ']));
@readfile ($row [' FileName ']);
Exit (0);
}else{
Header ("Location:/");
Exit
}
Six, by email to display the user's Gravatar avatar
$gravatar _link = ' http://www.gravatar.com/avatar/'. MD5 ($comment _author_email). ' S=32 ';
Echo ' ';