Table of Contents
一 从网页中提取关键词
二 查找页面上的所有链接
三 创建数据URI
四 下载和保存远程图片到你的服务器
五 移除Microsoft Word HTML标签
六 检测浏览器语言
七 保存请求信息到本地
八 excel相互转换日期
9 json与数据相互转换
 
Home Backend Development PHP Tutorial 比较有用的php代码片断

比较有用的php代码片断

Jun 13, 2016 pm 12:28 PM
date gt html json

比较有用的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

     

     

     

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles