Home > Backend Development > PHP Tutorial > 比较有用的php代码片断

比较有用的php代码片断

WBOY
Release: 2016-06-13 12:28:49
Original
861 people have browsed it

比较有用的php代码片段

一 从网页中提取关键词

$meta = get_meta_tags(<span style="color: #800000;">'</span><span style="color: #800000;">http://www.emoticode.net/</span><span style="color: #800000;">'</span><span style="color: #000000;">);$keywords </span>= $meta[<span style="color: #800000;">'</span><span style="color: #800000;">keywords</span><span style="color: #800000;">'</span><span style="color: #000000;">];</span><span style="color: #008000;">//</span><span style="color: #008000;"> Split keywords</span>$keywords = explode(<span style="color: #800000;">'</span><span style="color: #800000;">,</span><span style="color: #800000;">'</span><span style="color: #000000;">, $keywords );</span><span style="color: #008000;">//</span><span style="color: #008000;"> Trim them</span>$keywords = array_map( <span style="color: #800000;">'</span><span style="color: #800000;">trim</span><span style="color: #800000;">'</span><span style="color: #000000;">, $keywords );</span><span style="color: #008000;">//</span><span style="color: #008000;"> Remove empty values</span>$keywords =<span style="color: #000000;"> array_filter( $keywords );print_r( $keywords );</span>
Copy after login

二 查找页面上的所有链接

<span style="color: #000000;">使用DOM,你可以在任意页面上抓取链接,示例如下。</span>
Copy after login
  1. $html = file_get_contents('http://www.example.com');
  2.  
  3. $dom = new DOMDocument();
  4. @$dom->loadHTML($html);
  5.  
  6. // grab all the on the page
  7. $xpath = new DOMXPath($dom);
  8. $hrefs = $xpath->evaluate("/html/body//a");
  9.  
  10. for ($i = 0; $i $hrefs->length; $i++) {
  11. $href = $hrefs->item($i);
  12. $url = $href->getAttribute('href');
  13. echo $url.'
    ';
  14. }
    Copy after login

    三 创建数据URI

    数据URI可以帮助将图像嵌入到HTML/CSS/<span style="color: #000000;">JS中,从而节省HTTP请求。下面的函数可以利用$file创建数据URI。 function data_uri($file, $mime) {    $contents</span>=<span style="color: #000000;">file_get_contents($file);    $base64</span>=<span style="color: #000000;">base64_encode($contents);    echo </span><span style="color: #800000;">"</span><span style="color: #800000;">data:$mime;base64,$base64</span><span style="color: #800000;">"</span><span style="color: #000000;">;}</span>
    Copy after login

    四 下载和保存远程图片到你的服务器

    <span style="color: #000000;">当你在搭建网站时,很可能会从远程服务器上下载图片保存到你自己的服务器上,下面的代码就可以帮助你实现这个功能。$image </span>= file_get_contents(<span style="color: #800000;">'</span><span style="color: #800000;">http://www.php100.com/image.jpg</span><span style="color: #800000;">'</span><span style="color: #000000;">);file_put_contents(</span><span style="color: #800000;">'</span><span style="color: #800000;">/images/image.jpg</span><span style="color: #800000;">'</span>, $image);   <span style="color: #008000;">//</span><span style="color: #008000;">Where to save the image</span>
    Copy after login

    五 移除Microsoft Word HTML标签

    <span style="color: #000000;"> 当你使用Microsoft Word时,会创建很多标签tag,比如font、span、style、class等,这些标签在Word中十分有用,但当你从Word中把文本粘贴到网页上,就会出现很多没用的标签。下面实用的函数可以帮助你清除所有的Word HTML标签。function cleanHTML($html) {</span><span style="color: #808080;">///</span> <span style="color: #808080;">///</span><span style="color: #008000;"> Removes all FONT and SPAN tags, and all Class and Style attributes.</span><span style="color: #808080;">///</span><span style="color: #008000;"> Designed to get rid of non-standard Microsoft Word HTML tags.</span><span style="color: #808080;">///</span> <span style="color: #008000;">//</span><span style="color: #008000;"> start by completely removing all unwanted tags</span><span style="color: #000000;">$html </span>= ereg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">]*></span><span style="color: #800000;">"</span>,<span style="color: #800000;">""</span><span style="color: #000000;">,$html);</span><span style="color: #008000;">//</span><span style="color: #008000;"> then run another pass over the html (twice), removing unwanted attributes</span><span style="color: #000000;">$html </span>= ereg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">]*)(class|lang|style|size|face)=(</span><span style="color: #800000;">"</span>[^<span style="color: #800000;">"</span><span style="color: #800000;">]*</span><span style="color: #800000;">"</span>|<span style="color: #800000;">'</span><span style="color: #800000;">[^</span><span style="color: #800000;">'</span>]*<span style="color: #800000;">'</span><span style="color: #800000;">|[^>]+)([^>]*)>","",$html);</span>$html = ereg_replace(<span style="color: #800000;">"</span><span style="color: #800000;">]*)(class|lang|style|size|face)=(</span><span style="color: #800000;">"</span>[^<span style="color: #800000;">"</span><span style="color: #800000;">]*</span><span style="color: #800000;">"</span>|<span style="color: #800000;">'</span><span style="color: #800000;">[^</span><span style="color: #800000;">'</span>]*<span style="color: #800000;">'</span><span style="color: #800000;">|[^>]+)([^>]*)>","",$html);</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $html}</span>
    Copy after login

    六 检测浏览器语言

    <span style="color: #000000;">如果你的网站是多种语言的,下面的代码可以帮助你检测浏览器语言,它会返回客户端浏览器的默认语言。function get_client_language($availableLanguages, $</span><span style="color: #0000ff;">default</span>=<span style="color: #800000;">'</span><span style="color: #800000;">en</span><span style="color: #800000;">'</span><span style="color: #000000;">){    </span><span style="color: #0000ff;">if</span> (isset($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT_LANGUAGE</span><span style="color: #800000;">'</span><span style="color: #000000;">])) {          $langs</span>=explode(<span style="color: #800000;">'</span><span style="color: #800000;">,</span><span style="color: #800000;">'</span>,$_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_ACCEPT_LANGUAGE</span><span style="color: #800000;">'</span><span style="color: #000000;">]);          </span><span style="color: #0000ff;">foreach</span> ($langs <span style="color: #0000ff;">as</span><span style="color: #000000;"> $value){              $choice</span>=substr($value,<span style="color: #800080;">0</span>,<span style="color: #800080;">2</span><span style="color: #000000;">);              </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(in_array($choice, $availableLanguages)){                    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $choice;              }          }      }       </span><span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">default</span><span style="color: #000000;">;}</span>
    Copy after login

    七 保存请求信息到本地

    file_put_contents(<span style="color: #800000;">'</span><span style="color: #800000;">/tmp/all.log</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">mapping</span><span style="color: #800000;">'</span>.date(<span style="color: #800000;">"</span><span style="color: #800000;">m-d H:i:s</span><span style="color: #800000;">"</span>).<span style="color: #800000;">"</span><span style="color: #800000;">\n</span><span style="color: #800000;">"</span>,FILE_APPEND);
    Copy after login

    八 excel相互转换日期

    <span style="color: #0000ff;">//如果去获取某个excel日期(格式为:2016-03-12),那么获取到的是数字,需要经过转换才能恢复<br>public</span> function excelTime($date, $time = <span style="color: #0000ff;">false</span><span style="color: #000000;">) {            </span><span style="color: #0000ff;">if</span>(function_exists(<span style="color: #800000;">'</span><span style="color: #800000;">GregorianToJD</span><span style="color: #800000;">'</span><span style="color: #000000;">)){                </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (is_numeric( $date )) {                $jd </span>= GregorianToJD( <span style="color: #800080;">1</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">1970</span><span style="color: #000000;"> );                $gregorian </span>= JDToGregorian( $jd + intval ( $date ) - <span style="color: #800080;">25569</span><span style="color: #000000;"> );                $date </span>= explode( <span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span><span style="color: #000000;">, $gregorian );                $date_str </span>= str_pad( $date [<span style="color: #800080;">2</span>], <span style="color: #800080;">4</span>, <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span><span style="color: #000000;">, STR_PAD_LEFT )                .</span><span style="color: #800000;">"</span><span style="color: #800000;">-</span><span style="color: #800000;">"</span>. str_pad( $date [<span style="color: #800080;">0</span>], <span style="color: #800080;">2</span>, <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span><span style="color: #000000;">, STR_PAD_LEFT )                .</span><span style="color: #800000;">"</span><span style="color: #800000;">-</span><span style="color: #800000;">"</span>. str_pad( $date [<span style="color: #800080;">1</span>], <span style="color: #800080;">2</span>, <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span><span style="color: #000000;">, STR_PAD_LEFT )                . ($time </span>? <span style="color: #800000;">"</span><span style="color: #800000;"> 00:00:00</span><span style="color: #800000;">"</span> : <span style="color: #800000;">''</span><span style="color: #000000;">);                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $date_str;                }            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{               </span><span style="color: #008000;">//</span><span style="color: #008000;"> $date=$date>25568? $date+1:25569;</span>                <span style="color: #008000;">/*</span><span style="color: #008000;">There was a bug if Converting date before 1-1-1970 (tstamp 0)</span><span style="color: #008000;">*/</span><span style="color: #000000;">                $ofs</span>=(<span style="color: #800080;">70</span> * <span style="color: #800080;">365</span> + <span style="color: #800080;">17</span>+<span style="color: #800080;">2</span>) * <span style="color: #800080;">86400</span><span style="color: #000000;">;                $date </span>= date(<span style="color: #800000;">"</span><span style="color: #800000;">Y-m-d</span><span style="color: #800000;">"</span>,($date * <span style="color: #800080;">86400</span>) - $ofs).($time ? <span style="color: #800000;">"</span><span style="color: #800000;"> 00:00:00</span><span style="color: #800000;">"</span> : <span style="color: #800000;">''</span><span style="color: #000000;">);                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $date;            }    }</span>
    Copy after login

    9 json与数据相互转换

    <span style="color: #800080;">1</span><span style="color: #000000;"> json转换成数组$json </span>= <span style="color: #800000;">'</span><span style="color: #800000;">[{"id":"22","name":"33","descn":"44"}]</span><span style="color: #800000;">'</span>; <span style="color: #008000;">//</span><span style="color: #008000;">json格式的数组转换成 php的数组</span>$arr =<span style="color: #000000;"> (Array)json_decode($json);echo $arr[</span><span style="color: #800080;">0</span>]->id; <span style="color: #008000;">//</span><span style="color: #008000;">用对象的方式访问(这种是没有转换成数组,而是转换成对象的情况</span>
    Copy after login
    <span style="color: #800080;">2</span><span style="color: #000000;"> 数组转换成json$json_arr </span>= array(<span style="color: #800000;">'</span><span style="color: #800000;">WebName</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">11</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">WebSite</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">11</span><span style="color: #800000;">'</span><span style="color: #000000;">);$php_json </span>= json_encode($json_arr); <span style="color: #008000;">//</span><span style="color: #008000;">把php数组格式转换成 json 格式的数据</span>echo $php_json;
    Copy after login

     

     

     

Related labels:
source:php.cn
Previous article:php 使用fopen与file_get_contents读取文件范例 Next article:使用PHP-Barcode轻巧生成条形码(一)
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template