Inhaltsverzeichnis
一 从网页中提取关键词
二 查找页面上的所有链接
三 创建数据URI
四 下载和保存远程图片到你的服务器
五 移除Microsoft Word HTML标签
六 检测浏览器语言
七 保存请求信息到本地
八 excel相互转换日期
9 json与数据相互转换
 
Heim Backend-Entwicklung 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>
Nach dem Login kopieren

二 查找页面上的所有链接

<span style="color: #000000;">使用DOM,你可以在任意页面上抓取链接,示例如下。</span>
Nach dem Login kopieren
  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. }
    Nach dem Login kopieren

    三 创建数据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>
    Nach dem Login kopieren

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

    <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>
    Nach dem Login kopieren

    五 移除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>
    Nach dem Login kopieren

    六 检测浏览器语言

    <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>
    Nach dem Login kopieren

    七 保存请求信息到本地

    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);
    Nach dem Login kopieren

    八 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>
    Nach dem Login kopieren

    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>
    Nach dem Login kopieren
    <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;
    Nach dem Login kopieren

     

     

     

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Tabellenrahmen in HTML Tabellenrahmen in HTML Sep 04, 2024 pm 04:49 PM

Anleitung zum Tabellenrahmen in HTML. Hier besprechen wir verschiedene Möglichkeiten zum Definieren von Tabellenrändern anhand von Beispielen für den Tabellenrand in HTML.

Verschachtelte Tabelle in HTML Verschachtelte Tabelle in HTML Sep 04, 2024 pm 04:49 PM

Dies ist eine Anleitung für verschachtelte Tabellen in HTML. Hier diskutieren wir anhand der entsprechenden Beispiele, wie man eine Tabelle innerhalb der Tabelle erstellt.

HTML-Rand links HTML-Rand links Sep 04, 2024 pm 04:48 PM

Anleitung zum HTML-Rand links. Hier besprechen wir einen kurzen Überblick über HTML margin-left und seine Beispiele sowie seine Code-Implementierung.

HTML-Tabellenlayout HTML-Tabellenlayout Sep 04, 2024 pm 04:54 PM

Leitfaden zum HTML-Tabellenlayout. Hier besprechen wir die Werte des HTML-Tabellenlayouts zusammen mit den Beispielen und Ausgaben im Detail.

HTML-Eingabeplatzhalter HTML-Eingabeplatzhalter Sep 04, 2024 pm 04:54 PM

Leitfaden für HTML-Eingabeplatzhalter. Hier besprechen wir die Beispiele für HTML-Eingabeplatzhalter zusammen mit den Codes und Ausgaben.

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

Anleitung zum Verschieben von Text in HTML. Hier besprechen wir eine Einführung, wie Marquee-Tags funktionieren, mit Syntax und Beispielen für die Implementierung.

HTML-geordnete Liste HTML-geordnete Liste Sep 04, 2024 pm 04:43 PM

Leitfaden zur HTML-geordneten Liste. Hier besprechen wir auch die Einführung von HTML-geordneten Listen und Typen sowie deren Beispiele

HTML-Onclick-Button HTML-Onclick-Button Sep 04, 2024 pm 04:49 PM

Anleitung zum HTML-OnClick-Button. Hier diskutieren wir deren Einführung, Funktionsweise, Beispiele und Onclick-Events in verschiedenen Veranstaltungen.

See all articles