作为一个PHP开发人员,经常收集一些代码片段是非常有益的,以备将来使用。这些代码片段可以节省您宝贵的时间,也可以提高您的工作效率。
1. 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;
- }
2. 清除对数据库的恶意代码输入
这是一个有用的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;
- }
3. 解压文件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';
- }
4. 从网页提取关键字
这是一个非常有用的代码片段,可从任何网页中提取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 );
5. 检查服务器是否是 HTTPS
这个PHP代码片段能够读取关于你服务器 SSL 启用(HTTPS)的相关信息。
- if ($_SERVER['HTTPS'] != "on") {
- echo "This is not HTTPS";
- }else{
- echo "This is HTTPS";
- }
6. 在任意网页显示源代码
这是简单的PHP代码,用于显示任何网页的源代码,包含行号。
- $lines = file('http://google.com/');
- foreach ($lines as $line_num => $line) {
- // loop thru each line and prepend line numbers
- echo "Line #<strong>{$line_num}</strong> : " . htmlspecialchars($line) . "
- \n";
- }
7. 创建数据的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";
- }
8. 取得一个页面中的所有链接
通过使用此代码段,您可以很容易地提取任何网页上的所有链接。
- $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.'
- ';
- }
9. 让网页标题变得对搜索引擎更友好
这是个非常有用的PHP函数,能够根据网页标题创建搜索引擎友好的URL。
- function make_seo_name($title) {
- return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title))));
- }
10. 下载和保存远程图片在你的服务器中
如果你想从一个特定的URL下载图像并保存到服务器上,那么这个代码片断刚好满足要求。
- $image = file_get_contents('http://www.url.com/image.jpg');
- file_put_contents('/images/image.jpg', $image); //save the image on your server