比较有用的php代码片段,_PHP教程

WBOY
發布: 2016-07-12 08:57:29
原創
731 人瀏覽過

比较有用的php代码片段,

一 从网页中提取关键词

$meta = get_meta_tags(<span>'</span><span>http://www.emoticode.net/</span><span>'</span><span>);
$keywords </span>= $meta[<span>'</span><span>keywords</span><span>'</span><span>];
</span><span>//</span><span> Split keywords</span>
$keywords = explode(<span>'</span><span>,</span><span>'</span><span>, $keywords );
</span><span>//</span><span> Trim them</span>
$keywords = array_map( <span>'</span><span>trim</span><span>'</span><span>, $keywords );
</span><span>//</span><span> Remove empty values</span>
$keywords =<span> array_filter( $keywords );

print_r( $keywords );</span>
登入後複製

二 查找页面上的所有链接

<span>使用DOM,你可以在任意页面上抓取链接,示例如下。

</span>
登入後複製
登入後複製

三 创建数据URI

数据URI可以帮助将图像嵌入到HTML/CSS/<span>JS中,从而节省HTTP请求。下面的函数可以利用$file创建数据URI。 

function data_uri($file, $mime) {
    $contents</span>=<span>file_get_contents($file);
    $base64</span>=<span>base64_encode($contents);
    echo </span><span>"</span><span>data:$mime;base64,$base64</span><span>"</span><span>;
}</span>
登入後複製

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

<span>当你在搭建网站时,很可能会从远程服务器上下载图片保存到你自己的服务器上,下面的代码就可以帮助你实现这个功能。

$image </span>= file_get_contents(<span>'</span><span>http://www.php100.com/image.jpg</span><span>'</span><span>);
file_put_contents(</span><span>'</span><span>/images/image.jpg</span><span>'</span>, $image);   <span>//</span><span>Where to save the image</span>
登入後複製

五 移除Microsoft Word HTML标签

<span> 当你使用Microsoft Word时,会创建很多标签tag,比如font、span、style、class等,这些标签在Word中十分有用,但当你从Word中把文本粘贴到网页上,就会出现很多没用的标签。下面实用的函数可以帮助你清除所有的Word HTML标签。

function cleanHTML($html) {
</span><span>///</span> 
<span>///</span><span> Removes all FONT and SPAN tags, and all Class and Style attributes.
</span><span>///</span><span> Designed to get rid of non-standard Microsoft Word HTML tags.
</span><span>///</span> 
<span>//</span><span> start by completely removing all unwanted tags</span>
<span>
$html </span>= ereg_replace(<span>"</span><span><(/)?(font|span|del|ins)[^>]*></span><span>"</span>,<span>""</span><span>,$html);

</span><span>//</span><span> then run another pass over the html (twice), removing unwanted attributes</span>
<span>
$html </span>= ereg_replace(<span>"</span><span><([^>]*)(class|lang|style|size|face)=(</span><span>"</span>[^<span>"</span><span>]*</span><span>"</span>|<span>'</span><span>[^</span><span>'</span>]*<span>'</span><span>|[^>]+)([^>]*)>","<\1>",$html);</span>
$html = ereg_replace(<span>"</span><span><([^>]*)(class|lang|style|size|face)=(</span><span>"</span>[^<span>"</span><span>]*</span><span>"</span>|<span>'</span><span>[^</span><span>'</span>]*<span>'</span><span>|[^>]+)([^>]*)>","<\1>",$html);</span>

<span>return</span><span> $html
}</span>
登入後複製

六 检测浏览器语言

<span>如果你的网站是多种语言的,下面的代码可以帮助你检测浏览器语言,它会返回客户端浏览器的默认语言。

function get_client_language($availableLanguages, $</span><span>default</span>=<span>'</span><span>en</span><span>'</span><span>){
    </span><span>if</span> (isset($_SERVER[<span>'</span><span>HTTP_ACCEPT_LANGUAGE</span><span>'</span><span>])) {
          $langs</span>=explode(<span>'</span><span>,</span><span>'</span>,$_SERVER[<span>'</span><span>HTTP_ACCEPT_LANGUAGE</span><span>'</span><span>]);

          </span><span>foreach</span> ($langs <span>as</span><span> $value){
              $choice</span>=substr($value,<span>0</span>,<span>2</span><span>);
              </span><span>if</span><span>(in_array($choice, $availableLanguages)){
                    </span><span>return</span><span> $choice;
              }
          }
      } 
      </span><span>return</span> $<span>default</span><span>;
}</span>
登入後複製

七 保存请求信息到本地

file_put_contents(<span>'</span><span>/tmp/all.log</span><span>'</span>,<span>'</span><span>mapping</span><span>'</span>.date(<span>"</span><span>m-d H:i:s</span><span>"</span>).<span>"</span><span>\n</span><span>"</span>,FILE_APPEND);
登入後複製

八 excel相互转换日期

<span>//如果去获取某个excel日期(格式为:2016-03-12),那么获取到的是数字,需要经过转换才能恢复<br />public</span> function excelTime($date, $time = <span>false</span><span>) {

            </span><span>if</span>(function_exists(<span>'</span><span>GregorianToJD</span><span>'</span><span>)){

                </span><span>if</span><span> (is_numeric( $date )) {

                $jd </span>= GregorianToJD( <span>1</span>, <span>1</span>, <span>1970</span><span> );

                $gregorian </span>= JDToGregorian( $jd + intval ( $date ) - <span>25569</span><span> );

                $date </span>= explode( <span>'</span><span>/</span><span>'</span><span>, $gregorian );

                $date_str </span>= str_pad( $date [<span>2</span>], <span>4</span>, <span>'</span><span>0</span><span>'</span><span>, STR_PAD_LEFT )

                .</span><span>"</span><span>-</span><span>"</span>. str_pad( $date [<span>0</span>], <span>2</span>, <span>'</span><span>0</span><span>'</span><span>, STR_PAD_LEFT )

                .</span><span>"</span><span>-</span><span>"</span>. str_pad( $date [<span>1</span>], <span>2</span>, <span>'</span><span>0</span><span>'</span><span>, STR_PAD_LEFT )

                . ($time </span>? <span>"</span><span> 00:00:00</span><span>"</span> : <span>''</span><span>);

                </span><span>return</span><span> $date_str;
                }

            }</span><span>else</span><span>{

               </span><span>//</span><span> $date=$date>25568? $date+1:25569;</span>

                <span>/*</span><span>There was a bug if Converting date before 1-1-1970 (tstamp 0)</span><span>*/</span><span>

                $ofs</span>=(<span>70</span> * <span>365</span> + <span>17</span>+<span>2</span>) * <span>86400</span><span>;

                $date </span>= date(<span>"</span><span>Y-m-d</span><span>"</span>,($date * <span>86400</span>) - $ofs).($time ? <span>"</span><span> 00:00:00</span><span>"</span> : <span>''</span><span>);
                </span><span>return</span><span> $date;
            }

    }</span>
登入後複製

9 json与数据相互转换

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

 

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1108335.htmlTechArticle比较有用的php代码片段, 一 从网页中提取关键词 $meta = get_meta_tags( ' http://www.emoticode.net/ ' );$keywords = $meta[ ' keywords ' ]; // Split keywords $keywo...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!