{"id":247,"date":"2013-08-15T17:19:56","date_gmt":"2013-08-15T17:19:56","guid":{"rendered":"http:\/\/blog.yyhcw.com\/post\/247.html"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T07:54:17","slug":"PHPcode","status":"publish","type":"post","link":"http:\/\/blog.yyhcw.com\/post\/247.html","title":{"rendered":"21\u4e2a\u975e\u5e38\u5b9e\u7528\u7684PHP\u4ee3\u7801 \u7a0b\u5e8f\u733f\u5fc5\u5907"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><p>\u7a0b\u5e8f\u733f\u975e\u5e38\u5b9e\u7528\u7684\u51e0\u4e2aPHP\u4ee3\u7801\uff0c\u7f51\u7edc\u6536\u96c6 \u4ee5\u540e\u4e5f\u4f1a\u4e0d\u65ad\u66f4\u65b0\uff01\u5f00\u59cb\u76f4\u64ad\uff1a<\/p>\n<p><strong>1. PHP\u53ef\u9605\u8bfb\u968f\u673a\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u3000\u3000\u6b64\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u53ef\u9605\u8bfb\u7684\u5b57\u7b26\u4e32\uff0c\u4f7f\u5176\u66f4\u63a5\u8fd1\u8bcd\u5178\u4e2d\u7684\u5355\u8bcd\uff0c\u5b9e\u7528\u4e14\u5177\u6709\u5bc6\u7801\u9a8c\u8bc1\u529f\u80fd\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">\/**************\r\n*@length - length of random string (must be a multiple of 2)\r\n**************\/\r\nfunction readable_random_string($length = 6){\r\n    $conso=array(\"b\",\"c\",\"d\",\"f\",\"g\",\"h\",\"j\",\"k\",\"l\",\r\n    \"m\",\"n\",\"p\",\"r\",\"s\",\"t\",\"v\",\"w\",\"x\",\"y\",\"z\");\r\n    $vocal=array(\"a\",\"e\",\"i\",\"o\",\"u\");\r\n    $password=\"\";\r\n    srand ((double)microtime()*1000000);\r\n    $max = $length\/2;\r\n    for($i=1; $i&lt;=$max; $i++)\r\n    {\r\n    $password.=$conso[rand(0,19)];\r\n    $password.=$vocal[rand(0,4)];\r\n    }\r\n    return $password;\r\n}<\/pre>\n<p><strong>2. PHP\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u3000\u3000\u5982\u679c\u4e0d\u9700\u8981\u53ef\u9605\u8bfb\u7684\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u6b64\u51fd\u6570\u66ff\u4ee3\uff0c\u5373\u53ef\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u4f5c\u4e3a\u7528\u6237\u7684\u968f\u673a\u5bc6\u7801\u7b49\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">\/*************\r\n*@l - length of random string\r\n*\/\r\nfunction generate_rand($l){\r\n  $c= \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\r\n  srand((double)microtime()*1000000);\r\n  for($i=0; $i&lt;$l; $i++) {\r\n      $rand.= $c[rand()%strlen($c)];\r\n  }\r\n  return $rand;\r\n }<\/pre>\n<p><strong>3. PHP\u7f16\u7801\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/p>\n<p>\u3000\u3000\u4f7f\u7528\u6b64\u4ee3\u7801\uff0c\u53ef\u4ee5\u5c06\u4efb\u4f55\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7f16\u7801\u4e3a HTML \u5b57\u7b26\u5b9e\u4f53\uff0c\u4ee5\u9632\u6b62\u88ab\u5783\u573e\u90ae\u4ef6\u7a0b\u5e8f\u6536\u96c6\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">function encode_email($email='info@domain.com', $linkText='Contact Us', $attrs ='class=\"emailencoder\"' )\r\n{\r\n    \/\/ remplazar aroba y puntos\r\n    $email = str_replace('@', '&amp;#64;', $email);\r\n    $email = str_replace('.', '&amp;#46;', $email);\r\n    $email = str_split($email, 5);  \r\n                  \r\n    $linkText = str_replace('@', '&amp;#64;', $linkText);\r\n    $linkText = str_replace('.', '&amp;#46;', $linkText);\r\n    $linkText = str_split($linkText, 5);  \r\n                  \r\n    $part1 = '&lt;a href=\"ma';\r\n    $part2 = 'ilto&amp;#58;';\r\n    $part3 = '\" '. $attrs .' &gt;';\r\n    $part4 = '&lt;\/a&gt;';  \r\n                  \r\n    $encoded = '&lt;script type=\"text\/javascript\"&gt;';\r\n    $encoded .= \"document.write('$part1');\";\r\n    $encoded .= \"document.write('$part2');\";\r\n    foreach($email as $e)\r\n    {\r\n            $encoded .= \"document.write('$e');\";\r\n    }\r\n    $encoded .= \"document.write('$part3');\";\r\n    foreach($linkText as $l)\r\n    {\r\n            $encoded .= \"document.write('$l');\";\r\n    }\r\n    $encoded .= \"document.write('$part4');\";\r\n    $encoded .= '&lt;\/script&gt;';  \r\n                  \r\n    return $encoded;\r\n}<\/pre>\n<p><strong>4. PHP\u9a8c\u8bc1\u90ae\u4ef6\u5730\u5740<\/strong><\/p>\n<p>\u3000\u3000\u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1\u4e5f\u8bb8\u662f\u4e2d\u6700\u5e38\u7528\u7684\u7f51\u9875\u8868\u5355\u9a8c\u8bc1\uff0c\u6b64\u4ee3\u7801\u9664\u4e86\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u4e5f\u53ef\u4ee5\u9009\u62e9\u68c0\u67e5\u90ae\u4ef6\u57df\u6240\u5c5e DNS \u4e2d\u7684 MX \u8bb0\u5f55\uff0c\u4f7f\u90ae\u4ef6\u9a8c\u8bc1\u529f\u80fd\u66f4\u52a0\u5f3a\u5927\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">function is_valid_email($email, $test_mx = false)\r\n{\r\n    if(eregi(\"^([_a-z0-9-]+)(\\.[_a-z0-9-]+)*@([a-z0-9-]+)(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$\", $email))\r\n        if($test_mx)\r\n        {\r\n            list($username, $domain) = split(\"@\", $email);\r\n            return getmxrr($domain, $mxrecords);\r\n        }\r\n        else\r\n            return true;\r\n    else\r\n        return false;\r\n}<\/pre>\n<p><strong>5. PHP\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong><\/p>\n<pre class=\"brush:php;toolbar:false;\">function list_files($dir)\r\n{\r\n    if(is_dir($dir))\r\n    {\r\n        if($handle = opendir($dir))\r\n        {\r\n            while(($file = readdir($handle)) !== false)\r\n            {\r\n                if($file != \".\" &amp;&amp; $file != \"..\" &amp;&amp; $file != \"Thumbs.db\")\r\n                {\r\n                    echo '&lt;a target=\"_blank\" href=\"'.$dir.$file.'\"&gt;'.$file.'&lt;\/a&gt;&lt;br&gt;'.\"\\n\";\r\n                }\r\n            }\r\n            closedir($handle);\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>6. PHP\u9500\u6bc1\u76ee\u5f55<\/strong><\/p>\n<p>\u3000\u3000\u5220\u9664\u4e00\u4e2a\u76ee\u5f55\uff0c\u5305\u62ec\u5b83\u7684\u5185\u5bb9\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">\/*****\r\n*@dir - Directory to destroy\r\n*@virtual[optional]- whether a virtual directory\r\n*\/\r\nfunction destroyDir($dir, $virtual = false)\r\n{\r\n    $ds = DIRECTORY_SEPARATOR;\r\n    $dir = $virtual ? realpath($dir) : $dir;\r\n    $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;\r\n    if (is_dir($dir) &amp;&amp; $handle = opendir($dir))\r\n    {\r\n        while ($file = readdir($handle))\r\n        {\r\n            if ($file == '.' || $file == '..')\r\n            {\r\n                continue;\r\n            }\r\n            elseif (is_dir($dir.$ds.$file))\r\n            {\r\n                destroyDir($dir.$ds.$file);\r\n            }\r\n            else\r\n            {\r\n                unlink($dir.$ds.$file);\r\n            }\r\n        }\r\n        closedir($handle);\r\n        rmdir($dir);\r\n        return true;\r\n    }\r\n    else\r\n    {\r\n        return false;\r\n    }\r\n}<\/pre>\n<p><strong>7. PHP\u89e3\u6790 JSON \u6570\u636e<\/strong><\/p>\n<p>\u3000\u3000\u4e0e\u5927\u591a\u6570\u6d41\u884c\u7684 Web \u670d\u52a1\u5982 Twitter \u901a\u8fc7\u5f00\u653e API \u6765\u63d0\u4f9b\u6570\u636e\u4e00\u6837\uff0c\u5b83\u603b\u662f\u80fd\u591f\u77e5\u9053\u5982\u4f55\u89e3\u6790 API \u6570\u636e\u7684\u5404\u79cd\u4f20\u9001\u683c\u5f0f\uff0c\u5305\u62ec JSON\uff0cXML \u7b49\u7b49\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">$json_string='{\"id\":1,\"name\":\"foo\",\"email\":\"foo@foobar.com\",\"interest\":[\"wordpress\",\"php\"]} ';\r\n$obj=json_decode($json_string);\r\necho $obj-&gt;name; \/\/prints foo\r\necho $obj-&gt;interest[1]; \/\/prints php<\/pre>\n<p><strong>8. PHP\u89e3\u6790 XML \u6570\u636e<\/strong><\/p>\n<pre class=\"brush:php;toolbar:false;\">\/\/xml string\r\n$xml_string=\"&lt;?xml version='1.0'?&gt;\r\n&lt;users&gt;\r\n   &lt;user id='398'&gt;\r\n      &lt;name&gt;Foo&lt;\/name&gt;\r\n      &lt;email&gt;foo@bar.com&lt;\/name&gt;\r\n   &lt;\/user&gt;\r\n   &lt;user id='867'&gt;\r\n      &lt;name&gt;Foobar&lt;\/name&gt;\r\n      &lt;email&gt;foobar@foo.com&lt;\/name&gt;\r\n   &lt;\/user&gt;\r\n&lt;\/users&gt;\";  \r\n            \r\n\/\/load the xml string using simplexml\r\n$xml = simplexml_load_string($xml_string);  \r\n            \r\n\/\/loop through the each node of user\r\nforeach ($xml-&gt;user as $user)\r\n{\r\n   \/\/access attribute\r\n   echo $user['id'], '  ';\r\n   \/\/subnodes are accessed by -&gt; operator\r\n   echo $user-&gt;name, '  ';\r\n   echo $user-&gt;email, '&lt;br \/&gt;';\r\n}<\/pre>\n<p><strong>9. PHP\u521b\u5efa\u65e5\u5fd7\u7f29\u7565\u540d<\/strong><\/p>\n<p>\u3000\u3000\u521b\u5efa\u7528\u6237\u53cb\u597d\u7684\u65e5\u5fd7\u7f29\u7565\u540d\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">function create_slug($string){\r\n    $slug=preg_replace('\/[^A-Za-z0-9-]+\/', '-', $string);\r\n    return $slug;\r\n}<\/pre>\n<p><strong>10. PHP\u83b7\u53d6\u5ba2\u6237\u7aef\u771f\u5b9e IP \u5730\u5740<\/strong><\/p>\n<p>\u3000\u3000\u8be5\u51fd\u6570\u5c06\u83b7\u53d6\u7528\u6237\u7684\u771f\u5b9e IP \u5730\u5740\uff0c\u5373\u4fbf\u4ed6\u4f7f\u7528\u4ee3\u7406\u670d\u52a1\u5668\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">function getRealIpAddr()\r\n{\r\n    if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))\r\n    {\r\n        $ip=$_SERVER['HTTP_CLIENT_IP'];\r\n    }\r\n    elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))\r\n    \/\/to check ip is pass from proxy\r\n    {\r\n        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];\r\n    }\r\n    else\r\n    {\r\n        $ip=$_SERVER['REMOTE_ADDR'];\r\n    }\r\n    return $ip;\r\n}<\/pre>\n<p><strong>11. PHP\u5f3a\u5236\u6027\u6587\u4ef6\u4e0b\u8f7d<\/strong><\/p>\n<p>\u3000\u3000\u4e3a\u7528\u6237\u63d0\u4f9b\u5f3a\u5236\u6027\u7684\u6587\u4ef6\u4e0b\u8f7d\u529f\u80fd\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">\/********************\r\n*@file - path to file\r\n*\/\r\nfunction force_download($file)\r\n{\r\n    if ((isset($file))&amp;&amp;(file_exists($file))) {\r\n       header(\"Content-length: \".filesize($file));\r\n       header('Content-Type: application\/octet-stream');\r\n       header('Content-Disposition: attachment; filename=\"' . $file . '\"');\r\n       readfile(\"$file\");\r\n    } else {\r\n       echo \"No file selected\";\r\n    }\r\n}<\/pre>\n<p><strong>12. PHP\u521b\u5efa\u6807\u7b7e\u4e91<\/strong><\/p>\n<pre class=\"brush:php;toolbar:false;\">function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )\r\n{\r\n    $minimumCount = min( array_values( $data ) );\r\n    $maximumCount = max( array_values( $data ) );\r\n    $spread       = $maximumCount - $minimumCount;\r\n    $cloudHTML    = '';\r\n    $cloudTags    = array();  \r\n          \r\n    $spread == 0 &amp;&amp; $spread = 1;  \r\n          \r\n    foreach( $data as $tag =&gt; $count )\r\n    {\r\n        $size = $minFontSize + ( $count - $minimumCount )\r\n            * ( $maxFontSize - $minFontSize ) \/ $spread;\r\n        $cloudTags[] = '&lt;a style=\"font-size: ' . floor( $size ) . 'px'\r\n        . '\" href=\"#\" title=\"\\'' . $tag  .\r\n        '\\' returned a count of ' . $count . '\"&gt;'\r\n        . htmlspecialchars( stripslashes( $tag ) ) . '&lt;\/a&gt;';\r\n    }  \r\n          \r\n    return join( \"\\n\", $cloudTags ) . \"\\n\";\r\n}\r\n\/**************************\r\n****   Sample usage    ***\/\r\n$arr = Array('Actionscript' =&gt; 35, 'Adobe' =&gt; 22, 'Array' =&gt; 44, 'Background' =&gt; 43,\r\n    'Blur' =&gt; 18, 'Canvas' =&gt; 33, 'Class' =&gt; 15, 'Color Palette' =&gt; 11, 'Crop' =&gt; 42,\r\n    'Delimiter' =&gt; 13, 'Depth' =&gt; 34, 'Design' =&gt; 8, 'Encode' =&gt; 12, 'Encryption' =&gt; 30,\r\n    'Extract' =&gt; 28, 'Filters' =&gt; 42);\r\necho getCloud($arr, 12, 36);<\/pre>\n<p><strong>13. PHP\u5bfb\u627e\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u76f8\u4f3c\u6027<\/strong><\/p>\n<p>\u3000\u3000PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u6781\u5c11\u4f7f\u7528\u7684 similar_text \u51fd\u6570\uff0c\u4f46\u6b64\u51fd\u6570\u975e\u5e38\u6709\u7528\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u76f8\u4f3c\u7a0b\u5ea6\u7684\u767e\u5206\u6bd4\u3002\u5b9e\u7528\u65f6\u5224\u65ad$percent\u7684\u767e\u5206\u6bd4\u5c31\u53ef\u4ee5\u4e86\uff01<\/p>\n<pre class=\"brush:php;toolbar:false;\">similar_text($string1, $string2, $percent);\r\n\/\/$percent will have the percentage of similarity<\/pre>\n<p><strong>14. PHP\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Gravatar \u901a\u7528\u5934\u50cf<\/strong><\/p>\n<p>\u3000\u3000\u968f\u7740 WordPress \u8d8a\u6765\u8d8a\u666e\u53ca\uff0cGravatar \u4e5f\u968f\u4e4b\u6d41\u884c\u3002\u7531\u4e8e Gravatar \u63d0\u4f9b\u4e86\u6613\u4e8e\u4f7f\u7528\u7684 API\uff0c\u5c06\u5176\u7eb3\u5165\u5e94\u7528\u7a0b\u5e8f\u4e5f\u53d8\u5f97\u5341\u5206\u65b9\u4fbf\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">\/******************\r\n*@email - Email address to show gravatar for\r\n*@size - size of gravatar\r\n*@default - URL of default gravatar to use\r\n*@rating - rating of Gravatar(G, PG, R, X)\r\n*\/\r\nfunction show_gravatar($email, $size, $default, $rating)\r\n{\r\n    echo '&lt;img src=\"http:\/\/www.gravatar.com\/avatar.php?gravatar_id='.md5($email).\r\n        '&amp;default='.$default.'&amp;size='.$size.'&amp;rating='.$rating.'\" width=\"'.$size.'px\"\r\n        height=\"'.$size.'px\" \/&gt;';\r\n}<\/pre>\n<p><strong>15. PHP\u5728\u5b57\u7b26\u65ad\u70b9\u5904\u622a\u65ad\u6587\u5b57<\/strong><\/p>\n<p>\u3000\u3000\u6240\u8c13\u65ad\u5b57 (word break)\uff0c\u5373\u4e00\u4e2a\u5355\u8bcd\u53ef\u5728\u8f6c\u884c\u65f6\u65ad\u5f00\u7684\u5730\u65b9\u3002\u8fd9\u4e00\u51fd\u6570\u5c06\u5728\u65ad\u5b57\u5904\u622a\u65ad\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">\/\/ Original PHP code by Chirp Internet: www.chirp.com.au\r\n\/\/ Please acknowledge use of this code by including this header.\r\nfunction myTruncate($string, $limit, $break=\".\", $pad=\"...\") {\r\n    \/\/ return with no change if string is shorter than $limit\r\n    if(strlen($string) &lt;= $limit)\r\n        return $string;   \r\n      \r\n    \/\/ is $break present between $limit and the end of the string?\r\n    if(false !== ($breakpoint = strpos($string, $break, $limit))) {\r\n        if($breakpoint &lt; strlen($string) - 1) {\r\n            $string = substr($string, 0, $breakpoint) . $pad;\r\n        }\r\n    }\r\n    return $string;\r\n}\r\n\/***** Example ****\/\r\n$short_string=myTruncate($long_string, 100, ' ');<\/pre>\n<p><strong>16. PHP\u6587\u4ef6 Zip \u538b\u7f29<\/strong><\/p>\n<pre class=\"brush:php;toolbar:false;\">\/* creates a compressed zip file *\/\r\nfunction create_zip($files = array(),$destination = '',$overwrite = false) {\r\n    \/\/if the zip file already exists and overwrite is false, return false\r\n    if(file_exists($destination) &amp;&amp; !$overwrite) { return false; }\r\n    \/\/vars\r\n    $valid_files = array();\r\n    \/\/if files were passed in...\r\n    if(is_array($files)) {\r\n        \/\/cycle through each file\r\n        foreach($files as $file) {\r\n            \/\/make sure the file exists\r\n            if(file_exists($file)) {\r\n                $valid_files[] = $file;\r\n            }\r\n        }\r\n    }\r\n    \/\/if we have good files...\r\n    if(count($valid_files)) {\r\n        \/\/create the archive\r\n        $zip = new ZipArchive();\r\n        if($zip-&gt;open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {\r\n            return false;\r\n        }\r\n        \/\/add the files\r\n        foreach($valid_files as $file) {\r\n            $zip-&gt;addFile($file,$file);\r\n        }\r\n        \/\/debug\r\n        \/\/echo 'The zip archive contains ',$zip-&gt;numFiles,' files with a status of ',$zip-&gt;status;  \r\n      \r\n        \/\/close the zip -- done!\r\n        $zip-&gt;close();  \r\n      \r\n        \/\/check to make sure the file exists\r\n        return file_exists($destination);\r\n    }\r\n    else\r\n    {\r\n        return false;\r\n    }\r\n}\r\n\/***** Example Usage ***\/\r\n$files=array('file1.jpg', 'file2.jpg', 'file3.gif');\r\ncreate_zip($files, 'myzipfile.zip', true);<\/pre>\n<p><strong>17. PHP\u89e3\u538b\u7f29 Zip \u6587\u4ef6<\/strong><\/p>\n<pre class=\"brush:php;toolbar:false;\">\/**********************\r\n*@file - path to zip file\r\n*@destination - destination directory for unzipped files\r\n*\/\r\nfunction unzip_file($file, $destination){\r\n    \/\/ create object\r\n    $zip = new ZipArchive() ;\r\n    \/\/ open archive\r\n    if ($zip-&gt;open($file) !== TRUE) {\r\n        die (\u2019Could not open archive\u2019);\r\n    }\r\n    \/\/ extract contents to destination directory\r\n    $zip-&gt;extractTo($destination);\r\n    \/\/ close archive\r\n    $zip-&gt;close();\r\n    echo 'Archive extracted to directory';\r\n}<\/pre>\n<p><strong>18. PHP\u4e3a URL \u5730\u5740\u9884\u8bbe http \u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u3000\u3000\u6709\u65f6\u9700\u8981\u63a5\u53d7\u4e00\u4e9b\u8868\u5355\u4e2d\u7684\u7f51\u5740\u8f93\u5165\uff0c\u4f46\u7528\u6237\u5f88\u5c11\u6dfb\u52a0 http:\/\/ \u5b57\u6bb5\uff0c\u6b64\u4ee3\u7801\u5c06\u4e3a\u7f51\u5740\u6dfb\u52a0\u8be5\u5b57\u6bb5\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">if (!preg_match(\"\/^(http|ftp):\/\", $_POST['url'])) {\r\n   $_POST['url'] = 'http:\/\/'.$_POST['url'];\r\n}<\/pre>\n<p><strong>19. PHP\u5c06\u7f51\u5740\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u8d85\u7ea7\u94fe\u63a5<\/strong><\/p>\n<p>\u3000\u3000\u8be5\u51fd\u6570\u5c06 URL \u548c E-mail \u5730\u5740\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u53ef\u70b9\u51fb\u7684\u8d85\u7ea7\u94fe\u63a5\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">function makeClickableLinks($text) {\r\n $text = eregi_replace('(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_+.~#?&amp;\/\/=]+)',\r\n '&lt;a href=\"\\1\"&gt;\\1&lt;\/a&gt;', $text);\r\n $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&amp;\/\/=]+)',\r\n '\\1&lt;a href=\"http:\/\/\\2\"&gt;\\2&lt;\/a&gt;', $text);\r\n $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})',\r\n '&lt;a href=\"mailto:\\1\"&gt;\\1&lt;\/a&gt;', $text);  \r\n    \r\nreturn $text;\r\n}<\/pre>\n<p><strong>20. PHP\u8c03\u6574\u56fe\u50cf\u5c3a\u5bf8<\/strong><\/p>\n<p>\u3000\u3000\u521b\u5efa\u56fe\u50cf\u7f29\u7565\u56fe\u9700\u8981\u8bb8\u591a\u65f6\u95f4\uff0c\u6b64\u4ee3\u7801\u5c06\u6709\u52a9\u4e8e\u4e86\u89e3\u7f29\u7565\u56fe\u7684\u903b\u8f91\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">\/**********************\r\n*@filename - path to the image\r\n*@tmpname - temporary path to thumbnail\r\n*@xmax - max width\r\n*@ymax - max height\r\n*\/\r\nfunction resize_image($filename, $tmpname, $xmax, $ymax)\r\n{\r\n    $ext = explode(\".\", $filename);\r\n    $ext = $ext[count($ext)-1];  \r\n    \r\n    if($ext == \"jpg\" || $ext == \"jpeg\")\r\n        $im = imagecreatefromjpeg($tmpname);\r\n    elseif($ext == \"png\")\r\n        $im = imagecreatefrompng($tmpname);\r\n    elseif($ext == \"gif\")\r\n        $im = imagecreatefromgif($tmpname);  \r\n    \r\n    $x = imagesx($im);\r\n    $y = imagesy($im);  \r\n    \r\n    if($x &lt;= $xmax &amp;&amp; $y &lt;= $ymax)\r\n        return $im;  \r\n    \r\n    if($x &gt;= $y) {\r\n        $newx = $xmax;\r\n        $newy = $newx * $y \/ $x;\r\n    }\r\n    else {\r\n        $newy = $ymax;\r\n        $newx = $x \/ $y * $newy;\r\n    }  \r\n    \r\n    $im2 = imagecreatetruecolor($newx, $newy);\r\n    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);\r\n    return $im2;\r\n}<\/pre>\n<p><strong>21. PHP\u68c0\u6d4b Ajax \u8bf7\u6c42<\/strong><\/p>\n<p>\u3000\u3000\u5927\u591a\u6570\u7684 JavaScript \u6846\u67b6\u5982 jQuery\uff0cMootools \u7b49\uff0c\u5728\u53d1\u51fa Ajax \u8bf7\u6c42\u65f6\uff0c\u90fd\u4f1a\u53d1\u9001\u989d\u5916\u7684 HTTP_X_REQUESTED_WITH \u5934\u90e8\u4fe1\u606f\uff0c\u5934\u5f53\u4ed6\u4eec\u4e00\u4e2aajax\u8bf7\u6c42\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u7aef\u4fa6\u6d4b\u5230 Ajax \u8bf7\u6c42\u3002<\/p>\n<pre class=\"brush:php;toolbar:false;\">if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){\r\n    \/\/If AJAX Request Then\r\n}else{\r\n\/\/something else\r\n}<\/pre>\n<p><\/p>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"\u7a0b\u5e8f\u733f\u975e\u5e38\u5b9e\u7528\u7684\u51e0\u4e2aPHP\u4ee3\u7801\uff0c\u7f51\u7edc\u6536\u96c6 \u4ee5\u540e\u4e5f\u4f1a\u4e0d\u65ad\u66f4\u65b0\uff01\u5f00\u59cb\u76f4\u64ad\uff1a 1. PHP\u53ef\u9605\u8bfb\u968f\u673a\u5b57\u7b26\u4e32 \u3000\u3000\u6b64\u4ee3 \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"views":314,"_links":{"self":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/247"}],"collection":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/comments?post=247"}],"version-history":[{"count":0,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/247\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}