Welcome Guest, Not a member yet? Register   Sign In
Getting certain keywords from a string ?

I'm implementing my webboard using CI and thinking about using some important words from the thread title as a page keyword. I thought about putting those "important words" I want to look for into an array, then foreach and check if any of them exists in the given string.

$str="Some thread titile with important words";
function get_key($str){

$key=array("important", "words", "I'm", "looking", "for");
foreach($key as $k){
    if(strpos($str, $k)!==FALSE)
        $ret.=$k.', ';
return $ret;

The function would return "important, words", and I'll put it in the META tag.

The concept should work, but there are too many words I want to put in the array (more that 50, I guess). The script would take some times to return the keywords.

What do you think about this? Are there any other functions that can do this?

Thanks Smile

Keep your keywords in a plain text file with 1 word on each line and load the file.
Once loaded create an array from the file by loading each line into an array element.
You may think this would take some time but honestly file access with PHP is super fast, and a 50 line file is nothing...I load SQL files with 200,000 + lines of SQL.

If you insist on doing it the array way, you could
- Explode the string on a space
- use array_intersect on that and the key workds array
- implode the resulting array with commas


array_intersect << Oh, I haven't thought about that one. I kept thinking only about str_xxx function.

That is worth trying. thanks Smile

Array_intersect (and its variants) are hidden gems Smile
I used to always do stuff like that manually using a for loop until I doscovered it (via these forums of course Wink)

Theme © iAndrew 2016 - Forum software by © MyBB