国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php實用代碼片段整理

2024-05-04 23:52:13
字體:
來源:轉載
供稿:網友

本文整理歸納了php實用代碼片段。分享給大家供大家參考,具體如下:

一 從網頁中提取關鍵詞

$meta = get_meta_tags('http://m.survivalescaperooms.com/');$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 );

二 查找頁面上的所有鏈接

使用DOM,你可以在任意頁面上抓取鏈接,示例如下。

$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 />';}

三 創建數據URI

數據URI可以幫助將圖像嵌入到HTML/CSS/JS中,從而節省HTTP請求。下面的函數可以利用$file創建數據URI。

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

四 下載和保存遠程圖片到你的服務器

當你在搭建網站時,很可能會從遠程服務器上下載圖片保存到你自己的服務器上,下面的代碼就可以幫助你實現這個功能。

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

五 移除Microsoft Word HTML標簽

當你使用Microsoft Word時,會創建很多標簽tag,比如font、span、style、class等,這些標簽在Word中十分有用,但當你從Word中把文本粘貼到網頁上,就會出現很多沒用的標簽。下面實用的函數可以幫助你清除所有的Word HTML標簽。

function cleanHTML($html) {////// Removes all FONT and SPAN tags, and all Class and Style attributes./// Designed to get rid of non-standard Microsoft Word HTML tags.///// 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}

六 檢測瀏覽器語言

如果你的網站是多種語言的,下面的代碼可以幫助你檢測瀏覽器語言,它會返回客戶端瀏覽器的默認語言。

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;}

七 保存請求信息到本地

復制代碼 代碼如下:
file_put_contents('/tmp/all.log','mapping'.date("m-d H:i:s")."/n",FILE_APPEND);

excel相互轉換日期

 

//如果去獲取某個excel日期(格式為:2016-03-12),那么獲取到的是數字,需要經過轉換才能恢復public function excelTime($date, $time = false) {  if(function_exists('GregorianToJD')){    if (is_numeric( $date )) {    $jd = GregorianToJD( 1, 1, 1970 );    $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );    $date = explode( '/', $gregorian );    $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )    ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )    ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )    . ($time ? " 00:00:00" : '');    return $date_str;    }  }else{    // $date=$date>25568? $date+1:25569;    /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/    $ofs=(70 * 365 + 17+2) * 86400;    $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');    return $date;  }}

九 json與數據相互轉換

1 json轉換成數組

$json = '[{"id":"22","name":"33","descn":"44"}]'; //json格式的數組轉換成 php的數組$arr = (Array)json_decode($json);echo $arr[0]->id; //用對象的方式訪問(這種是沒有轉換成數組,而是轉換成對象的情況

2 數組轉換成json

$json_arr = array('WebName'=>'11','WebSite'=>'11');$php_json = json_encode($json_arr); //把php數組格式轉換成 json 格式的數據echo $php_json;

希望本文所述對大家PHP程序設計有所幫助


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 龙海市| 区。| 宁德市| 拜城县| 巴马| 仪征市| 玛多县| 长顺县| 泽库县| 宝丰县| 安平县| 石家庄市| 和平区| 武义县| 光泽县| 勃利县| 安吉县| 台州市| 新化县| 信丰县| 读书| 茌平县| 宁阳县| 元江| 苍溪县| 昌黎县| 永平县| 如皋市| 玉田县| 安义县| 瑞丽市| 南安市| 丰台区| 无锡市| 陆河县| 蕉岭县| 温州市| 龙川县| 孟津县| 开远市| 沾益县|