作者:孤独中残存的幸福_627 | 来源:互联网 | 2018-03-22 17:14
这的确是一个很简单的功能,从一个PHP数组生成一个.csv文件。此函数使用 fputcsv PHP 内置函数生成逗号分隔文件(.CSV)。该函数有3个参数:数据,分隔符和CSV enclosure 默认是双引号。
2. [代码]使用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;
}
3. [代码]使用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';
}
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. [代码]在任意网页显示源代码
$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";
}
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://blog.0907.org');
$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(&#39;href&#39;);
echo $url.&#39;
&#39;;
}
9. [代码]让网页标题变得对搜索引擎更友好
function make_seo_name($title) {
return preg_replace(&#39;/[^a-z0-9_-]/i&#39;, &#39;&#39;, strtolower(str_replace(&#39; &#39;, &#39;-&#39;, trim($title))));
}
10. [代码]使用PHP下载和保存远程图片在你的服务器中。
$image = file_get_contents(&#39;http://blog.0907.org/wp-content/uploads/2014/03/xunlei.jpg&#39;);
file_put_contents(&#39;/images/image.jpg&#39;, $image); //save the image on your server