Search code examples
phpfrequencyphrase

Search Pattern Frequency


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


Solution

  • 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));
    }