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

[eluser]aquary[/eluser]
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.

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

$key=array("important", "words", "I'm", "looking", "for");
$ret='';
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
#2

[eluser]CrazyMerlin[/eluser]
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.
#3

[eluser]Dam1an[/eluser]
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

Smile
#4

[eluser]aquary[/eluser]
array_intersect << Oh, I haven't thought about that one. I kept thinking only about str_xxx function.

That is worth trying. thanks Smile
#5

[eluser]Dam1an[/eluser]
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