//load the xml string using simplexml $xml = simplexml_load_string($xml_string);
//loop through the each node of user foreach ($xml->user as $user) { //access attribute echo $user["id"], " "; //subnodes are accessed by -> operator echo $user->name, " "; echo $user->email, "<br />"; }
PHP 提供了一个极少使用的 similar_text 函数,但此函数非常有用,用于比较两个字符串并返回相似程度的百分比。 复制代码 代码如下: similar_text($string1, $string2, $percent); //$percent will have the percentage of similarity
14. 在应用程序中使用 Gravatar 通用头像
随着 WordPress 越来越普及,Gravatar 也随之流行。由于 Gravatar 提供了易于使用的 API,将其纳入应用程序也变得十分方便。 复制代码 代码如下: /****************** *@email - Email address to show gravatar for *@size - size of gravatar *@default - URL of default gravatar to use *@rating - rating of Gravatar(G, PG, R, X) */ function show_gravatar($email, $size, $default, $rating) { echo "<img src="http://www.gravatar.com/avatar.php?gravatar_id=".md5($email). "&default=".$default."&size=".$size."&rating=".$rating."" width="".$size."px" height="".$size."px" />"; }
15. 在字符断点处截断文字
所谓断字 (word break),即一个单词可在转行时断开的地方。这一函数将在断字处截断字符串。 复制代码 代码如下: // Original PHP code by Chirp Internet: www.chirp.com.au // Please acknowledge use of this code by including this header. function myTruncate($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string;
// is $break present between $limit and the end of the string? if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; } /***** Example ****/ $short_string=myTruncate($long_string, 100, " ");
16. 文件 Zip 压缩 复制代码 代码如下: /* creates a compressed zip file */ function create_zip($files = array(),$destination = "",$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo "The zip archive contains ",$zip->numFiles," files with a status of ",$zip->status;
//close the zip -- done! $zip->close();
//check to make sure the file exists return file_exists($destination); } else { return false; } } /***** Example Usage ***/ $files=array("file1.jpg", "file2.jpg", "file3.gif"); create_zip($files, "myzipfile.zip", true);
17. 解压缩 Zip 文件 复制代码 代码如下: /********************** *@file - path to zip file *@destination - destination directory for unzipped files */ 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"; }