10个实用的PHP代码片段

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

1. PHP数组生成 CSV 文件

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

  1. function generateCsv($data, $delimiter = ',', $enclosure = '"') {
  2. $handle = fopen('php://temp', 'r+');
  3. foreach ($data as $line) {
  4. fputcsv($handle, $line, $delimiter, $enclosure);
  5. }
  6. rewind($handle);
  7. while (!feof($handle)) {
  8. $contents .= fread($handle, 8192);
  9. }
  10. fclose($handle);
  11. return $contents;
  12. }

2. 清除对数据库的恶意代码输入

这是一个有用的PHP函数,可清理所有的输入数据,降低代码注入的几率。

  1. function sanitize_input_data($input_data) {
  2. $input_data = trim(htmlentities(strip_tags($input_data,“,”)));
  3. if (get_magic_quotes_gpc())
  4. $input_data = stripslashes($input_data);
  5. $input_data = mysql_real_escape_string($input_data);
  6. return $input_data;
  7. }

3. 解压文件Unzip

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

  1. function unzip_file($file, $destination) {
  2. // create object
  3. $zip = new ZipArchive() ;
  4. // open archive
  5. if ($zip->open($file) !== TRUE) {
  6. die ('Could not open archive');
  7. }
  8. // extract contents to destination directory
  9. $zip->extractTo($destination);
  10. // close archive
  11. $zip->close();
  12. echo 'Archive extracted to directory';
  13. }

4. 从网页提取关键字

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

  1. $meta = get_meta_tags('http://www.emoticode.net/');
  2. $keywords = $meta['keywords'];
  3. // Split keywords
  4. $keywords = explode(',', $keywords );
  5. // Trim them
  6. $keywords = array_map( 'trim', $keywords );
  7. // Remove empty values
  8. $keywords = array_filter( $keywords );
  9. print_r( $keywords );

5. 检查服务器是否是 HTTPS

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

  1. if ($_SERVER['HTTPS'] != "on") {
  2. echo "This is not HTTPS";
  3. }else{
  4. echo "This is HTTPS";
  5. }

6. 在任意网页显示源代码

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

  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 #<strong>{$line_num}</strong> : " . htmlspecialchars($line) . "
  5. \n";
  6. }

7. 创建数据的URI

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

  1. function data_uri($file, $mime) {
  2. $contents=file_get_contents($file);
  3. $base64=base64_encode($contents);
  4. echo "data:$mime;base64,$base64";
  5. }

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

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

  1. $html = file_get_contents('http://www.example.com');
  2. $dom = new DOMDocument();
  3. @$dom->loadHTML($html);
  4. // grab all the on the page
  5. $xpath = new DOMXPath($dom);
  6. $hrefs = $xpath->evaluate("/html/body//a");
  7. for ($i = 0; $i < $hrefs->length; $i++) {
  8. $href = $hrefs->item($i);
  9. $url = $href->getAttribute('href');
  10. echo $url.'
  11. ';
  12. }

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

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

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

10. 下载和保存远程图片在你的服务器中

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

  1. $image = file_get_contents('http://www.url.com/image.jpg');
  2. file_put_contents('/images/image.jpg', $image); //save the image on your server
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓