Bruno b b magalhães wrote:
Yes!!! DO NOT USE REGEXPS WHEN YOU DON'T NEED THEM! :) (not to be rude, but you *really* don't need them. You're doing SIMPLE str-replacements, which goes at LEAST a factor 20 faster using str_replace thant using *any* regexp function.)Hi People,
well, I am building a very sophisticated(?) CMS, and I am thinking to implement a keyword automatically generation function... I thought on the following structure:
==================
$submited_text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. ';
generate_keywords($submited_text);
function generate_keywords($text) { if(isset($text) && $text != '') { $words_to_ignore = array('/a/', '/to/', '/of/', '/from/' );
$words = str_word_count(preg_replace($words_to_ignore,'',$text),1);
foreach($words as $var=>$val)
{
$total[$val] = substr_count($text,$val);
}
}
}
=================
How can I sort the resulting array by value, without loosing its relations.
Is there a faster way of doing this?
Regards, Bruno B B Magalhaes
Just remember the following: from fastest to slowest: str_replace str_ireplace preg_replace ereg_replace eregi_replace
If you're not doing any regexp magic, use str_replace (or str_ireplace as of PHP 5). As quotes from the str_replace section of the PHP manual:
[snip]If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().[/snip].
Also, what does:
[snip]$words = str_word_count(preg_replace($words_to_ignore,'',$text),1);[/snip] do. The result is not used after storing it, nor is it returned in any way...?
Now, at the end you have the $total array, and you discard it at the end... how... useful? (void return)
hope those remarks help you, and if you consider me rude, just blame it on a very early shift I had today
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php