{"id":600,"date":"2012-04-25T11:15:25","date_gmt":"2012-04-25T03:15:25","guid":{"rendered":"http:\/\/zduo.me\/blog\/?p=600"},"modified":"2015-11-06T13:20:43","modified_gmt":"2015-11-06T05:20:43","slug":"php%e9%87%87%e9%9b%86%e7%a8%8b%e5%ba%8f%e7%9b%b8%e5%85%b3","status":"publish","type":"post","link":"http:\/\/zduo.me\/blog\/?p=600","title":{"rendered":"PHP\u91c7\u96c6\u7a0b\u5e8f\u76f8\u5173"},"content":{"rendered":"<p>\u91c7\u96c6\u7a0b\u5e8f\u6700\u7b80\u5355\u7684\u601d\u8def\u5c31\u662f\uff1a\u83b7\u53d6\u9875\u9762\u4ee3\u7801\u2014\u2014\u5206\u6790\u4ee3\u7801\u2014\u2014\u83b7\u53d6\u9700\u8981\u7684\u90e8\u5206\u2014\u2014\u5199\u5165\u6570\u636e\u5e93\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5728\u83b7\u53d6\u9875\u9762\u4ee3\u7801\u4e4b\u524d\u6211\u4eec\u9996\u5148\u8981\u83b7\u5f97\u88ab\u91c7\u96c6\u9875\u9762\u7684url\uff0c\u8fd9\u91cc\u6211\u4eec\u4ece\u767e\u5ea6\u65b0\u95fb\u83b7\u53d6\u67d0\u5173\u952e\u8bcd\u6700\u65b0\u65b0\u95fb\u5217\u8868\u3002<\/p>\n<pre name=\"code\" class=\"php\">function get_baidu_news_url($str){\r\n    if(!$str) return;\r\n    $str = gbk($str);\r\n\r\n    \/\/\u4f7f\u7528\u6b63\u5219\u5339\u914d\u51fa\u5217\u8868\u4e2d\u7684\u65b0\u95fbURL\r\n    $html = utf8(file_get_contents('http:\/\/news.baidu.com\/ns?tn=newstitle&amp;cl=2&amp;rn=20&amp;word=' . urlencode($str)));\r\n    preg_match_all(\"\/&lt;a href=http:\\\/\\\/(.+?)  mon=(.+?)&gt;(.+?)&lt;\\\/a&gt;\/is\",$html,$match);\r\n\r\n    $urlarr = $match[1];\r\n    if($urlarr){\r\n        \/\/URL\u524d\u9762\u8865\u4e0ahttp:\/\/\r\n        array_walk($new_urlarr, create_function('&amp;$v', '$v = \"http:\/\/\".$v ;'));\r\n        cache_write($cachename, $new_urlarr, CACHE_PATH);\r\n    }\r\n    return $new_urlarr;\r\n}<\/pre>\n<p>\u62ff\u5230\u65b0\u95fb\u9875\u9762\u7684URL\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u91c7\u96c6\u9875\u9762\u4e0a\u7684\u65b0\u95fb\uff0c\u5f53\u7136\u8fd9\u4e00\u6b65\u8981\u9488\u5bf9\u5404\u4e2a\u7f51\u7ad9\u5b9a\u5236\u4e0d\u540c\u7684\u5339\u914d\u89c4\u5219\uff0c\u8fd9\u91cc\u4ee5\u65b0\u6d6a\u4e3a\u4f8b\u3002\u65b0\u6d6a\u65b0\u95fb\u4ee5\u201c&lt;!&#8211; \u6b63\u6587\u5185\u5bb9 begin &#8211;&gt;\u201d\u5f00\u59cb\uff0c\u201c&lt;!&#8211; \u6b63\u6587\u5185\u5bb9 end &#8211;&gt;\u201d\u7ed3\u675f\uff0c\u6240\u4ee5\u5339\u914d\u6b63\u6587\u7684\u6b63\u5219\u4e3a<\/p>\n<pre name=\"code\" class=\"php\">preg_match_all(\"\/&lt;!-- \u6b63\u6587\u5185\u5bb9 begin --&gt;([\\s|\\S]*?)&lt;!-- \u6b63\u6587\u5185\u5bb9 end --&gt;\/is\",$html,$match);<\/pre>\n<p>\u540c\u7406\uff0c\u5339\u914d\u6807\u9898\u7684\u6b63\u5219\u4e3a<\/p>\n<pre name=\"code\" class=\"php\">preg_match_all(\"\/&lt;h1 id=\\\"artibodyTitle\\\"(.*?)&gt;(.+?)&lt;\\\/h1&gt;\/is\",$html,$title_match);<\/pre>\n<p>\u51fd\u6570\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n<pre name=\"code\" class=\"php\">function news_content_sina($url){\r\n    if(!$url) return;\r\n    $html = utf8(file_get_contents($url));\r\n    preg_match_all(\"\/&lt;!-- \u6b63\u6587\u5185\u5bb9 begin --&gt;([\\s|\\S]*?)&lt;!-- \u6b63\u6587\u5185\u5bb9 end --&gt;\/is\",$html,$match);\r\n preg_match_all(\"\/&lt;h1 id=\\\"artibodyTitle\\\"(.*?)&gt;(.+?)&lt;\\\/h1&gt;\/is\",$html,$title_match);\r\n\r\n    $title = $title_match[2][0];\r\n    $content = news_content_format($match[1][0]);\r\n    if($title &amp;&amp; $content) return compact('url','title','content');\r\n}<\/pre>\n<p>\u4e0a\u9762\u65b9\u6cd5\u4e2d\u7528\u5230\u4e86 news_content_format()\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662f\u7528\u6765\u683c\u5f0f\u5316\u91c7\u96c6\u5230\u7684\u6b63\u6587\uff0c\u53bb\u6389js\u3001css\u3001iframe\u3001\u6ce8\u91ca\u4ee5\u53ca\u7ad9\u5185\u94fe\u63a5\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u53bb\u9664\u4e86\u7ad9\u5185\u94fe\u63a5\u4f46\u4fdd\u7559\u4e86\u5176\u4ed6\u7684&lt;a&gt;\u6807\u7b7e\u94fe\u63a5\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u67d0\u4e9b\u91cd\u8981\u5916\u94fe\u4e22\u5931\u5f71\u54cd\u65b0\u95fb\u5b8c\u6574\u6027\uff0c\u6bd4\u5982\u67d0\u8003\u8bd5\u62a5\u540d\u5730\u5740\u94fe\u63a5\u3002<\/p>\n<pre name=\"code\" class=\"php\">function news_content_format($content){\r\n    $search = array (\"'&lt;script[^&gt;]*?&gt;.*?&lt;\/script&gt;'si\",   \/\/ \u53bb\u6389 javascript\r\n                    \"'&lt;style[^&gt;]*?&gt;.*?&lt;\/style&gt;'si\",   \/\/ \u53bb\u6389 css\r\n                    \"'&lt;iframe[^&gt;]*?&gt;.*?&lt;\/iframe&gt;'si\",   \/\/ \u53bb\u6389 iframe\r\n                    \"'&lt;!--[\/!]*?[^&lt;&gt;]*?&gt;'si\",           \/\/ \u53bb\u6389 \u6ce8\u91ca \u6807\u8bb0\r\n     \"'&lt;a(.*?) href=(.*?)(sina.com|weibo.com)(.*?)&gt;(.*?)&lt;\\\/a&gt;'si\",  \/\/ \u53bb\u6389 \u5185\u90e8 \u94fe\u63a5\r\n    );\r\n    $replace = array (\"\",\"\",\"\",\"\",\"\",\"\\${5}\"); \r\n    $content = preg_replace($search, $replace, $content);\r\n $content = strip_tags($content, '&lt;p&gt; &lt;br&gt; &lt;img&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;strong&gt; &lt;a&gt;');\r\n return $content;\r\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u91c7\u96c6\u7684\u529f\u80fd\u57fa\u672c\u4e0a\u5c31\u5b8c\u6210\u4e86\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u70b9\u8981\u6ce8\u610f\uff0c\u5c31\u662f\u65b0\u95fb\u4e2d\u7684\u56fe\u7247\u5982\u679c\u4e5f\u9700\u8981\u91c7\u96c6\u5230\u672c\u5730\u7684\u8bdd\uff0c\u5c31\u8981\u7528\u5230\u4e0b\u9762\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<pre name=\"code\" class=\"php\">function getimage($body){\r\n global $img_dir,$img_webhost;\/\/\u672c\u7ad9\u56fe\u7247\u76ee\u5f55\u3001\u4e3b\u673a\r\n\r\n if(!empty($body)){  \r\n  $body = stripslashes($body);\r\n  $img_array = array();\r\n        \/\/\u5339\u914d\u51fa\u91c7\u96c6\u5185\u5bb9\u4e2d\u7684\u56fe\u7247\u5730\u5740\r\n  preg_match_all(\"\/(src|SRC)=[\\\"|'|]{0,}(http:\\\/\\\/(.*)\\.(gif|jpg|jpeg|bmp|png))\/isU\",$body,$img_array);\r\n  $img_array = array_unique($img_array[2]);\r\n  set_time_limit(0);\r\n        \/\/\u5b9a\u4e49\u56fe\u7247\u540d\uff0c\u521b\u5efa\u76ee\u5f55\r\n  $imgPath = $img_dir.\"\/\".strftime(\"%Y%m%d\",time());\r\n  $imgUrl = \"http:\/\/\".$img_webhost.\"\/\".$imgPath;\r\n  $milliSecond = strftime(\"%H%M%S\",time());\r\n  if(!is_dir($imgPath)) @mkdir($imgPath,0777);\r\n  foreach($img_array as $key =&gt;$value)\r\n  {\r\n   $value = trim($value);\r\n            \/\/\u6293\u53d6\u56fe\u7247\u5e76\u4fdd\u5b58\r\n   $get_file = @file_get_contents($value);\r\n   $rndFileName = $imgPath.\"\/\".$milliSecond.$key.\".\".substr($value,-3,3);\r\n   $fileurl = $imgUrl.\"\/\".$milliSecond.$key.\".\".substr($value,-3,3);\r\n   if($get_file)\r\n   {\r\n    $fp = @fopen($rndFileName,\"w\");\r\n    @fwrite($fp,$get_file);\r\n    @fclose($fp);\r\n   }\r\n   $body = ereg_replace($value,$fileurl,$body);\r\n  }\r\n  return $body;\r\n }<\/pre>\n<p>\u5199\u5165\u6570\u636e\u5e93\u7b49\u5e38\u89c4\u7684\u64cd\u4f5c\u5728\u8fd9\u91cc\u5c31\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u91c7\u96c6\u7a0b\u5e8f\u6700\u7b80\u5355\u7684\u601d\u8def\u5c31\u662f\uff1a\u83b7\u53d6\u9875\u9762\u4ee3\u7801\u2014\u2014\u5206\u6790\u4ee3\u7801\u2014\u2014\u83b7\u53d6\u9700\u8981\u7684\u90e8\u5206\u2014\u2014\u5199\u5165\u6570\u636e\u5e93\u3002 \u5f53\u7136\uff0c\u5728\u83b7\u53d6\u9875\u9762\u4ee3\u7801\u4e4b\u524d\u6211\u4eec\u9996\u5148\u8981\u83b7\u5f97\u88ab\u91c7\u96c6\u9875\u9762\u7684url\uff0c\u8fd9\u91cc\u6211\u4eec\u4ece\u767e\u5ea6\u65b0\u95fb\u83b7\u53d6\u67d0\u5173\u952e\u8bcd\u6700\u65b0\u65b0\u95fb\u5217\u8868\u3002 function get_baidu_news_url($str){ if(!$str) return; $str = gbk($str); \/\/\u4f7f\u7528\u6b63\u5219\u5339\u914d\u51fa\u5217\u8868\u4e2d\u7684\u65b0\u95fbURL $html = utf8(file_get_contents(&#8216;http:\/\/news.baidu.com\/ns?tn=newstitle&amp;cl=2&amp;rn=20&amp;word=&#8217; . urlencode($str))); preg_match_all(&#8220;\/&lt;a href=http:\\\/\\\/(.+?) mon=(.+?)&gt;(.+?)&lt;\\\/a&gt;\/is&#8221;,$html,$match); $urlarr = $match[1]; if($urlarr){ \/\/URL\u524d\u9762\u8865\u4e0ahttp:\/\/ array_walk($new_urlarr, create_function(&#8216;&amp;$v&#8217;, &#8216;$v = &#8220;http:\/\/&#8221;.$v ;&#8217;)); cache_write($cachename, $new_urlarr, CACHE_PATH); } return $new_urlarr; } \u62ff\u5230\u65b0\u95fb\u9875\u9762\u7684URL\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u91c7\u96c6\u9875\u9762\u4e0a\u7684\u65b0\u95fb\uff0c\u5f53\u7136\u8fd9\u4e00\u6b65\u8981\u9488\u5bf9\u5404\u4e2a\u7f51\u7ad9\u5b9a\u5236\u4e0d\u540c\u7684\u5339\u914d\u89c4\u5219\uff0c\u8fd9\u91cc\u4ee5\u65b0\u6d6a\u4e3a\u4f8b\u3002\u65b0\u6d6a\u65b0\u95fb\u4ee5\u201c&lt;!&#8211; \u6b63\u6587\u5185\u5bb9 begin &#8211;&gt;\u201d\u5f00\u59cb\uff0c\u201c&lt;!&#8211; &hellip; <a href=\"http:\/\/zduo.me\/blog\/?p=600\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/600"}],"collection":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=600"}],"version-history":[{"count":2,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":646,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/600\/revisions\/646"}],"wp:attachment":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}