我使用 php 类从文章中制作标签云,但我想删除只有 3 个字符或更少的单词,也删除数字单词。
示例标签: 1111 猴 鹿 猫 猪 水牛
我想要结果:猴鹿水牛
该类的 PHP 代码(此处为完整代码)
function keywords_extract($text)
{
$text = strtolower($text);
$text = strip_tags($text);
/*
* Handle common words first because they have punctuation and we need to remove them
* before removing punctuation.
*/
$commonWords = "'tis,'twas,a,able,about,across,after,ain't,all,almost,also,am,among,an,and,any,are,aren't," .
"as,at,be,because,been,but,by,can,can't,cannot,could,could've,couldn't,dear,did,didn't,do,does,doesn't," .
"don't,either,else,ever,every,for,from,get,got,had,has,hasn't,have,he,he'd,he'll,he's,her,hers,him,his," .
"how,how'd,how'll,how's,however,i,i'd,i'll,i'm,i've,if,in,into,is,isn't,it,it's,its,just,least,let,like," .
"likely,may,me,might,might've,mightn't,most,must,must've,mustn't,my,neither,no,nor,not,o'clock,of,off," .
"often,on,only,or,other,our,own,rather,said,say,says,shan't,she,she'd,she'll,she's,should,should've," .
"shouldn't,since,so,some,than,that,that'll,that's,the,their,them,then,there,there's,these,they,they'd," .
"they'll,they're,they've,this,tis,to,too,twas,us,wants,was,wasn't,we,we'd,we'll,we're,were,weren't,what," .
"what'd,what's,when,when,when'd,when'll,when's,where,where'd,where'll,where's,which,while,who,who'd," .
"who'll,who's,whom,why,why'd,why'll,why's,will,with,won't,would,would've,wouldn't,yet,you,you'd,you'll," .
$commonWords = strtolower($commonWords);
$commonWords = explode(",", $commonWords);
foreach($commonWords as $commonWord)
{
$text = $this->str_replace_word($commonWord, "", $text);
}
/* remove punctuation and newlines */
/*
* Changed to handle international characters
*/
if ($this->m_bUTF8)
$text = preg_replace('/[^\p{L}0-9\s]|\n|\r/u',' ',$text);
else
$text = preg_replace('/[^a-zA-Z0-9\s]|\n|\r/',' ',$text);
/* remove extra spaces created */
$text = preg_replace('/ +/',' ',$text);
$text = trim($text);
$words = explode(" ", $text);
foreach ($words as $value)
{
$temp = trim($value);
if (is_numeric($temp))
continue;
$keywords[] = trim($temp);
}
return $keywords;
}
我尝试了各种方法,例如 use if (strlen($words)<3 && is_numeric($words)==true)
,但没有奏效。
请帮我