Home > Backend Development > PHP Tutorial > Super practical PHP code snippets, _PHP tutorial

Super practical PHP code snippets, _PHP tutorial

WBOY
Release: 2016-07-13 10:17:24
Original
973 people have browsed it

Super practical PHP code snippets,

1. Check whether the email has been read

When you send an email, you may want to know whether the email has been read by the other party. Here’s a very interesting snippet of code that displays the actual date and time the record was read by the other party’s IP address.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <? error_reporting(0); Header("Content-Type: image/jpeg");   //Get IP if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; }   //Time<code class="php comments">//Time $actual_time<code class="php variable">$actual_time = time();<code class="php plain">= time(); $actual_day<code class="php variable">$actual_day = <code class="php plain">= date<code class="php functions">date(<code class="php plain">('Y.m.d'<code class="php string">'Y.m.d', <code class="php plain">, $actual_time<code class="php variable">$actual_time);<code class="php plain">); $actual_day_chart<code class="php variable">$actual_day_chart = <code class="php plain">= date<code class="php functions">date(<code class="php plain">('d/m/y'<code class="php string">'d/m/y', <code class="php plain">, $actual_time<code class="php variable">$actual_time);<code class="php plain">); $actual_hour<code class="php variable">$actual_hour = <code class="php plain">= date<code class="php functions">date(<code class="php plain">('H:i:s'<code class="php string">'H:i:s', <code class="php plain">, $actual_time<code class="php variable">$actual_time);<code class="php plain">);   //GET Browser<code class="php comments">//GET Browser $browser<code class="php variable">$browser = <code class="php plain">= $_SERVER<code class="php variable">$_SERVER[<code class="php plain">['HTTP_USER_AGENT'<code class="php string">'HTTP_USER_AGENT'];<code class="php plain">];     <code class="php spaces">   //LOG<code class="php comments">//LOG $myFile<code class="php variable">$myFile = <code class="php plain">= "log.txt"<code class="php string">"log.txt";<code class="php plain">; $fh<code class="php variable">$fh = <code class="php plain">= fopen<code class="php functions">fopen(<code class="php plain">($myFile<code class="php variable">$myFile, <code class="php plain">, 'a+'<code class="php string">'a+');<code class="php plain">); $stringData<code class="php variable">$stringData = <code class="php plain">= $actual_day<code class="php variable">$actual_day . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. $actual_hour<code class="php variable">$actual_hour . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. $ip<code class="php variable">$ip . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. $browser<code class="php variable">$browser . <code class="php plain">. ' '<code class="php string">' ' . <code class="php plain">. "rn"<code class="php string">"rn";<code class="php plain">; fwrite(<code class="php plain">fwrite($fh<code class="php variable">$fh, <code class="php plain">, $stringData<code class="php variable">$stringData);<code class="php plain">); fclose(<code class="php plain">fclose($fh<code class="php variable">$fh);<code class="php plain">);   //Generate Image (Es. dimesion is 1x1)<code class="php comments">//Generate Image (Es. dimesion is 1x1) $newimage<code class="php variable">$newimage = ImageCreate(1,1);<code class="php plain">= ImageCreate(1,1); $grigio<code class="php variable">$grigio = ImageColorAllocate(<code class="php plain">= ImageColorAllocate($newimage<code class="php variable">$newimage,255,255,255);<code class="php plain">,255,255,255); ImageJPEG(<code class="php plain">ImageJPEG($newimage<code class="php variable">$newimage);<code class="php plain">); ImageDestroy(<code class="php plain">ImageDestroy($newimage<code class="php variable">$newimage);<code class="php plain">);     <code class="php spaces">   ?><code class="php plain">?>

Source code

2. Extract keywords from netizens

A great code snippet to easily extract keywords from web pages.

1 2 3 4 5 6 7 8 9 10 $meta = get_meta_tags('http://www.emoticode.net/'); $keywords = $meta['keywords']; // Split keywords $keywords = explode(',', $keywords ); // Trim them $keywords = array_map( 'trim', $keywords ); // Remove empty values $keywords = array_filter( $keywords );   print_r( $keywords );

Source code

3. Find all links on the page

Using DOM, you can easily grab links from any page. The code example is as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 $html = file_get_contents('http://www.example.com');   $dom = new DOMDocument(); @$dom->loadHTML($html);   // grab all the on the page $xpath = new DOMXPath($dom); $hrefs = $xpath->evaluate("/html/body//a");   for ($i = 0; $i < $hrefs->length; $i++) {        $href = $hrefs->item($i);        $url = $href->getAttribute('href');        echo $url.'<br />'; }

Source code

4. Automatically convert URL and jump to hyperlink

In WordPress, if you want to automatically convert the URL and jump to the hyperlinked page, you can use the built-in function make_clickable() to perform this operation. If you want to operate the program outside of WordPress, then you can refer to the wp-includes/formatting.php source code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 function _make_url_clickable_cb($matches) {     $ret = '';     $url = $matches[2];        if ( empty($url) )         return $matches[0];     // removed trailing [.,;:] from URL     if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {         $ret = substr($url, -1);         $url = substr($url, 0, strlen($url)-1);     }     return $matches[1] . "<a href="$url" rel="nofollow">$url</a>" . $ret; }  <code class="php spaces">   function<code class="php keyword">function _make_web_ftp_clickable_cb(_make_web_ftp_clickable_cb($matches$matches<code class="php plain">) {    ) {$ret     <code class="php plain">= $ret<code class="php string">'' ;=     ''<code class="php variable">$dest= ;$matches     <code class="php plain">[2];$dest<code class="php spaces">     $dest= <code class="php plain">= $matches<code class="php string">'http://'. [2];$dest     <code class="php plain">;$dest<code class="php spaces">      = if'http://'<code class="php plain">( empty. ($dest<code class="php variable">$dest) );          <code class="php keyword">return      <code class="php variable">$matchesif<code class="php plain">[0];     ( // removed trailing [,;:] from URLempty<code class="php spaces">    if(<code class="php plain">( in_array($dest<code class="php functions">substr() )$dest         <code class="php plain">, -1), return<code class="php keyword">array $matches<code class="php plain">('.'[0];,     <code class="php string">','// removed trailing [,;:] from URL<code class="php plain">,     <code class="php string">';'if<code class="php plain">, ':'( in_array()) === true ) {substr<code class="php spaces">        $ret(= $dest<code class="php functions">substr(, -1), $destarray<code class="php plain">, -1);        ($dest'.'<code class="php plain">= substr, (','<code class="php variable">$dest, 0, , strlen';'<code class="php plain">($dest, )-1);':'<code class="php spaces">    })) === true ) {             <code class="php keyword">return$ret<code class="php variable">$matches [1] . = "<a href="$dest" rel="nofollow">$dest</a>"substr<code class="php plain">. $ret(;$dest<code class="php plain">} , -1);function         <code class="php plain">_make_email_clickable_cb($dest<code class="php variable">$matches ) {=     substr<code class="php variable">$email= ($matches$dest<code class="php plain">[2] . '@', 0, . strlen<code class="php variable">$matches[3];(    $dest<code class="php keyword">return$matches)-1);[1] .     <code class="php string">"<a href="mailto:$email">$email</a>";}     <code class="php keyword">return $matches[1] . "<a href="$dest" rel="nofollow">$dest</a>" . $ret; }    function _make_email_clickable_cb($matches) {     $email = $matches[2] . '@' . $matches[3];     <code class="php keyword">return $matches[1] . "$email"; }  <code class="php spaces">   function<code class="php keyword">function make_clickable(<code class="php plain">make_clickable($ret<code class="php variable">$ret) {<code class="php plain">) {     <code class="php spaces">    $ret<code class="php variable">$ret = <code class="php plain">= ' '<code class="php string">' ' . <code class="php plain">. $ret<code class="php variable">$ret;<code class="php plain">;     <code class="php spaces">    // in testing, using arrays here was found to be faster<code class="php comments">// in testing, using arrays here was found to be faster     <code class="php spaces">    $ret<code class="php variable">$ret = preg_replace_callback(<code class="php plain">= preg_replace_callback('#([s>])([w]+?://[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is'<code class="php string">'#([s>])([w]+?://[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is', <code class="php plain">, '_make_url_clickable_cb'<code class="php string">'_make_url_clickable_cb', <code class="php plain">, $ret<code class="php variable">$ret);<code class="php plain">);     <code class="php spaces">    $ret<code class="php variable">$ret = preg_replace_callback(<code class="php plain">= preg_replace_callback('#([s>])((www|ftp).[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is'<code class="php string">'#([s>])((www|ftp).[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is', <code class="php plain">, '_make_web_ftp_clickable_cb'<code class="php string">'_make_web_ftp_clickable_cb', <code class="php plain">, $ret<code class="php variable">$ret);<code class="php plain">);     <code class="php spaces">    $ret<code class="php variable">$ret = preg_replace_callback(<code class="php plain">= preg_replace_callback('#([s>])([.0-9a-z_+-]+)@(([0-9a-z-]+.)+[0-9a-z]{2,})#i'<code class="php string">'#([s>])([.0-9a-z_+-]+)@(([0-9a-z-]+.)+[0-9a-z]{2,})#i', <code class="php plain">, '_make_email_clickable_cb'<code class="php string">'_make_email_clickable_cb', <code class="php plain">, $ret<code class="php variable">$ret);<code class="php plain">);  <code class="php spaces">       <code class="php spaces">    // this one is not in an array because we need it to run last, for cleanup of accidental links within links<code class="php comments">// this one is not in an array because we need it to run last, for cleanup of accidental links within links     <code class="php spaces">    $ret<code class="php variable">$ret = preg_replace(<code class="php plain">= preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i"<code class="php string">"#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", <code class="php plain">, "$1$3</a>"<code class="php string">"</a>", <code class="php plain">, $ret<code class="php variable">$ret);<code class="php plain">);     <code class="php spaces">    $ret<code class="php variable">$ret = trim(<code class="php plain">= trim($ret<code class="php variable">$ret);<code class="php plain">);     <code class="php spaces">    return<code class="php keyword">return $ret<code class="php variable">$ret;<code class="php plain">; }<code class="php plain">}

  源码

  五、创建数据URL

  数据URL可以直接嵌入到HTML/CSS/JS中,以节省大量的 HTTP请求。 下面的这段代码可利用$file轻松创建数据URL。

1 2 3 4 5
1 2 3 4 5 function data_uri($file, $mime) {   $contents=file_get_contents($file);   $base64=base64_encode($contents);   echo "data:$mime;base64,$base64"; }
function data_uri($file, $mime) {   $contents=file_get_contents($file);   $base64=base64_encode($contents);   echo "data:$mime;base64,$base64"; }

Source code

6. Download & save a remote image from the server

When you are building a website, you will download a certain image from a remote server and save it on your own server. This operation is often used. The code is as follows:

1 2 $image = file_get_contents('http://www.url.com/image.jpg'); file_put_contents('/images/image.jpg', $image); //Where to save the image

Source code

7. Remove Remove Microsoft Word HTML Tag

When you use Microsoft Word, you will create many tags, such as font, span, style, class, etc. These tags are very useful for Word itself, but when you paste from Word to a web page, you will find a lot of useless tags. Therefore, the following code helps you remove all useless Word HTML Tags.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function cleanHTML($html) { /// <summary> /// Removes all FONT and SPAN tags, and all Class and Style attributes. /// Designed to get rid of non-standard Microsoft Word HTML tags. /// </summary> // start by completely removing all unwanted tags   $html = ereg_replace("<(/)?(font|span|del|ins)[^>]*>","",$html);   // then run another pass over the html (twice), removing unwanted attributes   $html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html); $html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html);   return $html }

Source code

8. Detect browser language

If your website has multiple languages, you can use this code as the default language to detect the browser language. This code will return the initial language used by the browser client.

1 2 3 4 5 6 7 8 9 10 11 12 13 function get_client_language($availableLanguages, $default='en'){     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {         $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);           foreach ($langs as $value){             $choice=substr($value,0,2);             if(in_array($choice, $availableLanguages)){                 return $choice;             }         }     }     return $default; }

Source code

9. Display the number of Facebook fans

If you have an internally linked Facebook page on your website or blog, you may want to know how many fans you have. This code will help you check the number of Facebook fans. Remember, don’t forget to add this code on the second line of your page ID.

1 2 3 4 5 6 <?php $page_id = "YOUR PAGE-ID"; $xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot"); $fans = $xml->page->fan_count;     echo $fans; ?>

Source code

English source: Catswhocode

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/892522.htmlTechArticleSuper practical PHP code snippets, 1. Check whether the email has been read. When you are sending an email, you may I would like to know if the email has been read by the other party. There is a very interesting code here...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template