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('href'); echo $url.' '; }
9. [代码]让网页标题变得对搜索引擎更友好 function make_seo_name($title) { return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title)))); }
10. [代码]使用php下载和保存远程图片在你的服务器中。 $image = file_get_contents('http://blog.0907.org/wp-content/uploads/2014/03/xunlei.jpg'); file_put_contents('/images/image.jpg', $image); //save the image on your server