i use a php class to make tag cloud from article, but i want to remove words that only 3 character or less, also remove numeric words.
example tags: 1111 monkey deer cat pig buffalo
i want result : monkey deer buffalo
PHP code from that class (full code here)
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;
}
I’ve tried various ways, such as use if (strlen($words)<3 && is_numeric($words)==true), but it did not work.
please help me
I will slightly modify your process to make it run faster (I believe it should).
Step 1: Instead of replace each common word to empty string in
$text(replace process is expensive), I will store each common words into hash table for later filter.Step 2: Filter common word, numeric and words that contain less than 4 digits at the same time.
The following is a full source code for this function (in case you want to copy and paste)
Demo: ideone.com/obG6n