几个个实用的PHP代码片段【自己备份】

检查服务器是否是 HTTPS   

这个PHP代码片段能够读取关于你服务器 SSL 启用(HTTPS)信息。
if ($_SERVER['HTTPS'] != "on") { 
    echo "This is not HTTPS";
}else{
    echo "This is HTTPS";
}

在任意网页显示源代码

1 $lines = file('http://google.com/');
2 foreach ($lines as $line_num => $line) { 
3     // loop thru each line and prepend line numbers
4     echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "
5 \n";
6 }

创建数据的URI 

1 因为我们知道,数据URI可以将图像嵌入到HTML,CSS和JS以节省HTTP请求。这是一个非常实用的PHP代码片段来创建数据URI。
2 function data_uri($file, $mime) {
3   $contents=file_get_contents($file);
4   $base64=base64_encode($contents);
5   echo "data:$mime;base64,$base64";
6 }

取得一个页面中的所有链接 

 1 取得一个页面中的所有链接
 2 $html = file_get_contents('http://blog.0907.org');
 3  
 4 $dom = new DOMDocument();
 5 @$dom->loadHTML($html);
 6  
 7 // grab all the on the page
 8 $xpath = new DOMXPath($dom);
 9 $hrefs = $xpath->evaluate("/html/body//a");
10  
11 for ($i = 0; $i < $hrefs->length; $i++) {
12        $href = $hrefs->item($i);
13        $url = $href->getAttribute('href');
14        echo $url.'
15 ';
16 }

让网页标题变得对搜索引擎更友好 

1 function make_seo_name($title) {
2     return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title))));
3 }

使用PHP下载和保存远程图片在你的服务器中。

1 $image = file_get_contents('http://blog.0907.org/wp-content/uploads/2014/03/xunlei.jpg');
2 file_put_contents('/images/image.jpg', $image); //save the image on your server

 

posted @ 2014-11-07 10:19  cheungkaming  阅读(207)  评论(0编辑  收藏  举报