網上有很多 PHP 程式碼片段可以提高開發效率,也可以學習一下其中的技巧而應用在自己的項目中,下面 我愛水煮魚 就精選了幾個比較有用的 PHP 片段。
從網頁中提取關鍵詞
從指定頁面中提取關鍵詞並顯示出來。
| 代碼如下 |
複製代碼 |
$meta = get_meta_tags('http://www.bKjia.c0m/'); $keywords = $meta['keywords']; // 分割關鍵詞 $keywords = explode(',', $keywords ); // 整理 $keywords = array_map( 'trim', $keywords ); // 去掉空內容 $keywords = array_filter( $keywords ); print_r( $keywords ); |
得到頁面中所有的連結
下面代碼可以使用 PHP DOM 擷取指定頁面中的所有連結,僅作拋磚引玉,具體使用自由發揮。
| 代碼如下 |
複製代碼 |
$html = file_get_contents('http://www.hzhuti.com'); $dom = new DOMDocument(); @$dom->loadHTML($html); $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.' '; } |
自動把頁面中的 URL 轉換成可點擊的超連結
如果你發表一些文章或者做一些頁面,要想放上一個超連結,必須編寫一個 a 標籤。使用下面這段代碼可以方便的將 URL 轉換成超連結輸出。實現方法比較簡單,大體思路就是用正則匹配出來 URL 然後處理輸出超連結。
| 代碼如下 |
複製代碼 |
function _make_url_clickable_cb($matches) { $ret = ''; $url = $matches[2]; if ( empty($url) ) return $matches[0]; // 去掉 URL 後面的標點符號 if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) { $ret = substr($url, -1); $url = substr($url, 0, strlen($url)-1); } return $matches[1] . "$url" . $ret; } function _make_web_ftp_clickable_cb($matches) { $ret = ''; $dest = $matches[2]; $dest = 'http://' . $dest; if ( empty($dest) ) return $matches[0]; if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) { $ret = substr($dest, -1); $dest = substr($dest, 0, strlen($dest)-1); } return $matches[1] . "$dest" . $ret; } function _make_email_clickable_cb($matches) { $email = $matches[2] . '@' . $matches[3]; return $matches[1] . "$email"; } function make_clickable($ret) { $ret = ' ' . $ret; $ret = preg_replace_callback('#([s>])([w]+?://[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is', '_make_url_clickable_cb', $ret); $ret = preg_replace_callback('#([s>])((www|ftp).[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is', '_make_web_ftp_clickable_cb', $ret); $ret = preg_replace_callback('#([s>])([.0-9a-z_+-]+)@(([0-9a-z-]+.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret); $ret = preg_replace("#(]+?>|>))]+?>([^>]+?)#i", "$1$3", $ret); $ret = trim($ret); return $ret; } |
用 PHP 產生 Data URI 代碼
通常把圖片編碼成 Data URI 格式用在網頁中來減少 HTTP 要求來提升前端效能。同時還有一些其他的用途。下面代碼可以將檔案編碼成 Data 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.bKjia.c0m/logo.gif'); file_put_contents('/images/logo.gif', $image);
|
去掉文中的無用標籤
當從一些文字編輯器(例如 Word)中將文本複製到網頁編輯器中時,可能會有一些額外的無用標籤,例如一些指定文字樣式的 style 等。下面代碼可以通過正則匹配來去掉這些無用標籤,淨化文本:
| 代碼如下 |
複製代碼 |
function cleanHTML($html) { // 首先去掉無用的標籤(可以自訂更多需要清除的標籤) $html = ereg_replace("<(/)?(font|span|del|ins)[^>]*>","",$html); // 然後再運行兩遍去掉無用屬性 $html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html); $html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html); return $html } |
如果你也收藏了一些有用的 PHP 代碼
http://www.bkjia.com/PHPjc/632739.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632739.htmlTechArticle網上有很多 PHP 程式碼片段可以提高開發效率,也可以學習一下其中的技巧而應用在自己的項目中,下面 我愛水煮魚 就精選了幾個比較有用的...