数组中的substr_count

时间:2012-10-19 09:12:10

标签: php arrays count substr

我正在尝试在强标记中搜索关键字。

我为标签生成了一个数组,显示为:

        Array
        (
        [0] => Array
        (
        [0] =>  seo agency in london
        [1] => seo agency in london
        [2] => seo consultant in london
        [3] => seo consultant in london
        [4] => seo experts in london
        )

        [1] => Array
        (
        [0] =>  seo agency in london
        [1] => seo agency in london
        [2] => seo consultant in london
        [3] => seo consultant in london
        [4] => seo experts in london
        )

        )

我想搜索关键字,在这种情况下是“seo”

我正在使用:

function substr_count_array( $haystack, $needle ) {
$count = 0;
foreach ($needle as $substring) {
$count += substr_count( $haystack, $substring);
}
return $count;
}
$matchPattern7 = '/<strong(?:"(?:[^\\\"]|\\\.)*"|\'(?:[^\\\\\']|\\\.)*\'|[^\'">])*>(.*?)<\/strong>/';
preg_match_all($matchPattern7, $content, $foundIt7);
echo substr_count_array(strtolower($foundIt7), strtolower($keyword));

供参考..

$ keyword = seo

我变零,有人可以帮忙吗?感谢

2 个答案:

答案 0 :(得分:0)

function substr_count_array( $haystack, $needle ) {
$count = 0;
foreach ( $haystack as $substring) {
foreach($substring as $string)
{
$count += substr_count($string, $haystack);
}
}
return $count;
}

答案 1 :(得分:0)

经过测试并适合我:     

$foundIt7=array();
$foundIt7[1]=array(0 => ' seo agency in london',
        1 => 'seo agency in london',
        2 => 'seo consultant in london',
        3 => 'seo consultant in london',
        4 => 'seo experts in london');
print_r($foundIt7[1]);


function substr_count_array( $haystackAr, $needle ) {
$count = 0;
foreach ($haystackAr as $haystack) {
$count += substr_count( $haystack, $needle);
}
return $count;
}

echo substr_count_array($foundIt7[1],'seo');//returns 5