10个实用的PHP代码片段

jopen 10年前

作为一个PHP开发人员,经常收集一些代码片段是非常有益的,以备将来使用。这些代码片段可以节省您宝贵的时间,也提高您的工作效率。

 

从一个PHP数据生成 CSV 文件

这的确是一个很简单的功能,从一个PHP数组生成一个.csv文件。此函数使用 fputcsv PHP 内置函数生成逗号分隔文件(.CSV)。该函数有3个参数:数据,分隔符和CSV enclosure 默认是双引号。

function generateCsv($data, $delimiter = ',', $enclosure = '"') {     $handle = fopen('php://temp', 'r+');     foreach ($data as $line) {       fputcsv($handle, $line, $delimiter, $enclosure);     }     rewind($handle);     while (!feof($handle)) {       $contents .= fread($handle, 8192);     }     fclose($handle);     return $contents;  }

使用PHP对数据库输入进行恶意代码清除

这是一个有用的PHP函数清理了所有的输入数据,并删除代码注入的几率。

function sanitize_input_data($input_data) {   $input_data = trim(htmlentities(strip_tags($input_data,“,”)));   if (get_magic_quotes_gpc())   $input_data = stripslashes($input_data);   $input_data = mysql_real_escape_string($input_data);   return $input_data;  }

使用PHP解压文件Unzip

这是一个非常方便的PHP函数从。zip文件解压缩文件。它有两个参数:第一个是压缩文件的路径和第二个是目标文件的路径。

function unzip_file($file, $destination) {   // create object   $zip = new ZipArchive() ;   // open archive   if ($zip->open($file) !== TRUE) {    die ('Could not open archive');   }   // extract contents to destination directory   $zip->extractTo($destination);   // close archive   $zip->close();   echo 'Archive extracted to directory';  }

从网页提取的关键字

真的是一个非常有用的代码片段从任何网页中提取meta关键字。

$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 );

检查服务器是否是 HTTPS

这个PHP代码片段能够读取关于你服务器 SSL 启用(HTTPS)信息。

if ($_SERVER['HTTPS'] != "on") {    echo "This is not HTTPS";  }else{   echo "This is HTTPS";  }

在任意网页显示源代码

这是简单的PHP代码,用于显示任何网页的源代码包含行号。

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

 

创建数据的URI

因为我们知道,数据URI可以将图像嵌入到HTML,CSS和JS以节省HTTP请求。这是一个非常实用的PHP代码片段来创建数据URI。

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

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

通过使用此代码段中,您可以很容易地提取任何网页上的所有链接。

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

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

这是个非常有用的PHP函数,能够根据网页标题创建搜索引擎友好的URL。

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

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

如果你想从一个特定的URL下载图像并保存到服务器上,那么这个代码片断刚好满足要求。

$image = file_get_contents('http://www.url.com/image.jpg');  file_put_contents('/images/image.jpg', $image); //save the image on your server