{"id":225,"date":"2014-06-29T09:00:05","date_gmt":"2014-06-29T09:00:05","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=225"},"modified":"2014-06-29T09:00:05","modified_gmt":"2014-06-29T09:00:05","slug":"%e7%94%a8php%e5%ae%9e%e7%8e%b0%e4%bb%8e%e4%b8%80%e4%b8%aa%e6%96%87%e6%9c%ac%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e6%8f%90%e5%8f%96%e5%85%b3%e9%94%ae%e5%ad%97-%e6%8c%89%e5%87%ba%e7%8e%b0%e6%ac%a1","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/225.html","title":{"rendered":"\u7528PHP\u5b9e\u73b0\u4ece\u4e00\u4e2a\u6587\u672c\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5173\u952e\u5b57-\u6309\u51fa\u73b0\u6b21\u6570"},"content":{"rendered":"<div style=\"color: #000000;\"><span style=\"color: #000000; font-family: \u5b8b\u4f53; font-size: medium;\"><span style=\"color: #222222;\">\u4e00\u4e2a\u51fd\u6570\uff1a\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff08\u8fd8\u6709\u53e6\u5916\u51e0\u4e2a\u53ef\u9009\u53c2\u6570\uff09\uff0c\u7528\u4e8e\u5b9a\u4f4d\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5173\u952e\u5b57\uff08\u51fa\u73b0\u6700\u591a\u7684\u8bcd\uff09\uff0c\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u6216\u4e00\u4e2a\u7531\u9017\u53f7\u5206\u9694\u7684\u5173\u952e\u5b57\u7684\u5b57\u7b26\u4e32\uff1a<\/span><\/span><\/div>\n<div style=\"color: #000000;\">\n<pre class=\"lang:default decode:true \">&lt;?php\n\/**\n* Finds all of the keywords (words that appear most) on param $str\n* and return them in order of most occurrences to less occurrences.\n* @param string $str The string to search for the keywords.\n* @param int $minWordLen[optional] The minimun length (number of chars) of a word to be considered a keyword.\n* @param int $minWordOccurrences[optional] The minimun number of times a word has to appear\n* on param $str to be considered a keyword.\n* @param boolean $asArray[optional] Specifies if the function returns a string with the\n* keywords separated by a comma ($asArray = false) or a keywords array ($asArray = true).\n* @return mixed A string with keywords separated with commas if param $asArray is true,\n* an array with the keywords otherwise.\n*\/\nfunction extract_keywords($str, $minWordLen = 3, $minWordOccurrences = 2, $asArray = false) {\n    function keyword_count_sort($first, $sec) {\n        return $sec[1] - $first[1];\n    }\n    $str = preg_replace('\/[^\\w0-9 ]\/', ' ', $str);\n    $str = trim(preg_replace('\/s+\/', ' ', $str));\n\n    $words = explode(' ', $str);\n    $keywords = array();\n    while(($c_word = array_shift($words)) !== null) {\n        if(strlen($c_word) &lt;= $minWordLen) continue;\n\n        $c_word = strtolower($c_word);\n        if(array_key_exists($c_word, $keywords)) $keywords[$c_word][1]++;\n        else $keywords[$c_word] = array($c_word, 1);\n    }\n    usort($keywords, 'keyword_count_sort');\n\n    $final_keywords = array();\n    foreach($keywords as $keyword_det) {\n        if($keyword_det[1] &lt; $minWordOccurrences) break;\n        array_push($final_keywords, $keyword_det[0]);\n    }\n    return $asArray ? $final_keywords : implode(', ', $final_keywords);\n}\n\n\/\/How to use\n\n\/\/Basic lorem ipsum text to extract the keywords\n$text = \"\nLorem ipsum dolor sit amet, consectetur adipiscing elit.\nCurabitur eget ipsum ut lorem laoreet porta a non libero.\nVivamus in tortor metus. Suspendisse potenti. Curabitur\nmetus nisi, adipiscing eget placerat suscipit, suscipit\nvitae felis. Integer eu odio enim, sed dignissim lorem.\nIn fringilla molestie justo, vitae varius risus lacinia ac.\nNulla porttitor justo a lectus iaculis ut vestibulum magna\negestas. Ut sed purus et nibh cursus fringilla at id purus.\n\";\n\/\/Echoes: lorem, suscipit, metus, fringilla, purus, justo, eget, vitae, ipsum, curabitur, adipiscing\necho extract_keywords($text);<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div style=\"color: #000000;\"><\/div>\n<div style=\"color: #000000;\">\/\/\u8be5\u4ee3\u7801\u7247\u6bb5\u6765\u81ea\u4e8e:\u00a0<a href=\"http:\/\/www.sharejs.com\/codes\/php\/99\" target=\"_blank\">http:\/\/www.sharejs.com\/codes\/php\/99<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u51fd\u6570\uff1a\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff08\u8fd8\u6709\u53e6\u5916\u51e0\u4e2a\u53ef\u9009\u53c2\u6570\uff09\uff0c\u7528\u4e8e\u5b9a\u4f4d\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5173\u952e\u5b57\uff08\u51fa\u73b0\u6700\u591a\u7684\u8bcd\uff09\uff0c\u8fd4\u56de [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[48],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-php"],"views":2086,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}