0

Preferably PHP solutions -- but any ideas would be great.

Give a text blob

'This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times'

and a phrase list

'red sweaters, purple elephants'

want to search the text blob and return count of occurances

therefore

red sweaters = 3 and purple elephants = 2

cgmckeever
  • 3,923
  • 2
  • 18
  • 17

3 Answers3

4

http://www.php.net/manual/en/function.substr-count.php

$string = 'This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times';

$keys = 'red sweaters, purple elephants';

$pkeys = explode(', ', $keys);
foreach($pkeys as $key)
{
    printf("%s occourrences: %d\n", $key, substr_count($string, $key));
}
Strae
  • 18,807
  • 29
  • 92
  • 131
  • DaNieL - thanks.. I think I was over-engineering it!! Do you think that there is a benegit to substr_count over preg_match ? – cgmckeever Dec 06 '11 at 19:22
  • substr_count works differents from preg_match_all function - as noticed by amosrivera, substr_count() will hit, in you case, even brownred sweaters becose contain the string you use as needle. With preg_match_all you can define more complex (and precise) rules, for example you can do a case-insensitive search without modify the original string (as suggested Nick).. all depends on what do you need to do – Strae Dec 06 '11 at 20:59
2

You can use substr_count which will search for strings inside a text. Just note that in your example if the text was "brownred sweaters" that will count +1 for "red sweaters".

You can also use regular expressions. Something like preg_match("/$string/",$text);. This would return the times the string was found.

Also if you want to search for several strings delimited by a comma (like your example) you first need to split the string. You can use explode for this. $strings = explode(",",$search);

amosrivera
  • 26,114
  • 9
  • 67
  • 76
  • +1 for a more detailed answer than mine; even if regexp looks a bit overkill in this situation for me, your answer offer more solutions ;) – Strae Dec 06 '11 at 17:13
  • @DaNieL funny because i had upvoted your answer for being more straight forward ;) – amosrivera Dec 06 '11 at 17:21
1

Something like this should work:

<?php
  $string = strtolower('This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times');

  $allprases = 'red sweaters, purple elephants'

  $phrasearray = explode(',',$allphrases);

  foreach ($phrasearray as $k => $phrase) {
    $phrase = strtolower(trim($phrase));
    echo 'String '.$phrase.' found '.substr_count($string,$phrase).' times.<br />';
  }
?>

Do note that substr_count is case sensitive (which is why I'm strtolower()ing everything in the above code). This can be removed easily enough so that the code above is case sensitive too.

Nick
  • 6,316
  • 2
  • 29
  • 47