9個比較實用的php代碼片段
比較有用的php代碼片段,分享給大家供大家參考,具體代碼如下
一、從網(wǎng)頁中提取關(guān)鍵詞
$meta = get_meta_tags('http://www.emoticode.net/'); $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 />'; }
三、創(chuàng)建數(shù)據(jù)URI
數(shù)據(jù)URI可以幫助將圖像嵌入到HTML/CSS/JS中,從而節(jié)省HTTP請求。下面的函數(shù)可以利用$file創(chuàng)建數(shù)據(jù)URI。
function data_uri($file, $mime) { $contents=file_get_contents($file); $base64=base64_encode($contents); echo "data:$mime;base64,$base64"; }
四、下載和保存遠程圖片到你的服務(wù)器
當(dāng)你在搭建網(wǎng)站時,很可能會從遠程服務(wù)器上下載圖片保存到你自己的服務(wù)器上,下面的代碼就可以幫助你實現(xiàn)這個功能。
$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標(biāo)簽
當(dāng)你使用Microsoft Word時,會創(chuàng)建很多標(biāo)簽tag,比如font、span、style、class等,這些標(biāo)簽在Word中十分有用,但當(dāng)你從Word中把文本粘貼到網(wǎng)頁上,就會出現(xiàn)很多沒用的標(biāo)簽。下面實用的函數(shù)可以幫助你清除所有的Word HTML標(biāo)簽。
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 }
六、檢測瀏覽器語言
如果你的網(wǎng)站是多種語言的,下面的代碼可以幫助你檢測瀏覽器語言,它會返回客戶端瀏覽器的默認語言。
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; }
七、保存請求信息到本地
八、excel相互轉(zhuǎn)換日期
//如果去獲取某個excel日期(格式為:2016-03-12),那么獲取到的是數(shù)字,需要經(jīng)過轉(zhuǎn)換才能恢復(fù) 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與數(shù)據(jù)相互轉(zhuǎn)換
1 json轉(zhuǎn)換成數(shù)組 $json = '[{"id":"22","name":"33","descn":"44"}]'; //json格式的數(shù)組轉(zhuǎn)換成 php的數(shù)組 $arr = (Array)json_decode($json); echo $arr[0]->id; //用對象的方式訪問(這種是沒有轉(zhuǎn)換成數(shù)組,而是轉(zhuǎn)換成對象的情況
2 數(shù)組轉(zhuǎn)換成json $json_arr = array('WebName'=>'11','WebSite'=>'11'); $php_json = json_encode($json_arr); //把php數(shù)組格式轉(zhuǎn)換成 json 格式的數(shù)據(jù) echo $php_json;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo示例
這篇文章主要介紹了PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo,結(jié)合實例形式詳細分析了PHP基于協(xié)程的任務(wù)調(diào)度基本原理、定義及使用技巧,需要的朋友可以參考下2020-02-02PHP實現(xiàn)移除數(shù)組中為空或為某值元素的方法
這篇文章主要介紹了PHP實現(xiàn)移除數(shù)組中為空或為某值元素的方法,涉及php使用array_filter過濾數(shù)組的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01并發(fā)下常見的加鎖及鎖的PHP具體實現(xiàn)代碼
用到了Eaccelerator的內(nèi)存鎖 和 文件鎖,原理如下判斷系統(tǒng)中是否安了EAccelerator 如果有則使用內(nèi)存鎖,如果不存在,則進行文件鎖2010-10-10通過JavaScript或PHP檢測Android設(shè)備的代碼
在此列出一些能夠在iOS的最大競爭者——安卓(Android)系統(tǒng)的檢測方法。即通過JavaScript或PHP檢測Android設(shè)備,給大家提供參考。2011-03-03PHP實現(xiàn)防止表單重復(fù)提交功能【基于token驗證】
這篇文章主要介紹了PHP實現(xiàn)防止表單重復(fù)提交功能,結(jié)合實例形式分析了php基于token驗證防止表單重復(fù)提交的相關(guān)操作技巧,非常簡單實用,需要的朋友可以參考下2018-05-05