match string with array values

Posted on 2009-02-17
Last Modified: 2012-05-06
Hi Experts,
i have a string and an array with categories and targetnumbers.
Now i want to find out, how many times a category match with the given string.

Sorry it´s a german sentence. The Important words are "DJ" and "house".

Now the result with the code below is:
array(2) { [0]=> int(2) [1]=> int(4) }
And the result should be:
array(2) { [4]=> int(3) [2]=> int(1) }

The array key represent the "target" value from the array. The value of target should be the match count. The count for "DJ" or "DJs" is 3 and the count of "house" is 1.

$outputArray = array();

$string = "...GEORGE MOREL aus den USA wird eingeflogen und euch zu verstehen DJ geben warum er eine lebende House Legende ist. 

DJs und Produzenten aus aller Welt erweisen dem meistgebuchten US DJ der Welt Ihren Respekt.";

$matches = array();

$matches[] = array("category" => "sports", "target" => 1);

$matches[] = array("category" => "soccer", "target" => 1);

$matches[] = array("category" => "baseball", "target" => 1);

$matches[] = array("category" => "party", "target" => 2);

$matches[] = array("category" => "disco", "target" => 2);

$matches[] = array("category" => "house", "target" => 2);

$matches[] = array("category" => "concert", "target" => 3);

$matches[] = array("category" => "openair", "target" => 3);

$matches[] = array("category" => "DJ", "target" => 4);

$res = array();

foreach ($matches as $val)


	if (preg_match("/".$val["category"]."/i",$string))


		$res[] = $val["target"];





Open in new window

Question by:bibabutze
    1 Comment
    LVL 49

    Accepted Solution

    Hello bibabutze,

    Instead of preg_match, you could use preg_match_all:
    if(preg_match_all('/'.preg_quote($val['category'],'/').'/i', $string, $m)) {
      $count = count($m[0]);
      if(isset($res[$val['target']])) {
        $res[$val['target']]+= $count;
      } else {
        $res[$val['target']] = $count;



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now