1

OK here is what i want to do

I have an array that contains keywords

$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');

and i have another array that contains my whole titles

let's say

$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.','Cristiano Ronaldo is World Soccer's Player of the Year 2013.','Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona','','');

so now what i want to do

is to loop my keywords array in each of the titles array element

and if there is 3 keywords in one element then do something

for example

$titiles[0] // this one has these words => Madrid , cup club

so this one is have at least 3 words of my keywords

so if each element has 3 keywords or more , then echo that array element.

any idea on how to get this working?

1
  • 1
    loop titles, explode on space, run array_intersect() on the exploded vs $keywords count() result Commented May 27, 2014 at 0:43

3 Answers 3

4
foreach ($titles as $t){

$te=explode(' ',$t);

$c=count(array_intersect($te,$keywords));

if($c >=3){
echo $t.' has 3 or more matches';
}

} 

live demo: http://codepad.viper-7.com/7kUUEK

2 matches is your current max

if you want Madrid to match madrid

$keywords=array_map('strtolower', $keywords);
foreach ($titles as $t){

$te=explode(' ',$t);
$comp=array_map('strtolower', $te);

$c=count(array_intersect($comp,$keywords));



   if($c >=3){
   echo $t.' has 3 or more matches';
   }

} 

http://codepad.viper-7.com/itdegA

Sign up to request clarification or add additional context in comments.

1 Comment

Beat me to it by a mouse click :)
2

Alternatively, you could also use substr_count() to get the number of occurances. Consider this example:

$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');
$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.',"Cristiano Ronaldo is World Soccer's Player of the Year 2013.","Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona",'','');
$count = 0;
$data = array();
foreach($titles as $key => $value) {
    $value = strtolower($value);
    $keys = array_map('strtolower', $keywords);
    foreach($keys as $needle) {
        $count+= substr_count($value, $needle);
    }
    echo "In title[$key], the number of occurences using keywords = " .$count . '<br/>';
    $count = 0;
}

Sample Output:

In title[0], the number of occurences using keywords = 3
In title[1], the number of occurences using keywords = 2
In title[2], the number of occurences using keywords = 2
In title[3], the number of occurences using keywords = 0
In title[4], the number of occurences using keywords = 0

Fiddle

3 Comments

Thanks buddy , but why you wrote $count again at the end of the first foreach loop
@HasanZohdy Your welcome, it just resets the counter. foreach piece of the title there is each count. so after the iteration of a title you need to reset it to prepare it for the next one
just setting it once, before the 2nd foreach would make more sense to me.
1

Simpler with array_intersect:

$keywords = array('sport','messi','ronaldo','Barcelona','madrid','club','final','cup','player');

$titles = array('Real Madrid is the only club to have kept a European cup by winning it five times in a row.','Cristiano Ronaldo is World Soccer\'s Player of the Year 2013.','Lionel Messi Reaches $50 Million-A-Year Deal With Barcelona');

foreach($titles as $title) {
    if (count(array_intersect(explode(' ',strtolower($title)), $keywords)) >= 3) {
        //stuff
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.