我想找到一个sustring(基于模式)在另一个字符串中的出现次数。例如:
$mystring = "|graboard='KERALA'||graboarded='KUSAT'||graboard='MG'";
我想找到$mystring
、中存在的graboard
的编号
所以我使用了正则表达式,但我将如何找到不发生?
如果必须使用正则表达式,preg_match_all()
将返回匹配数。
使用preg_match_all
:
$mystring = "|graboard='KERALA'||graboarded='KUSAT'||graboard='MG'";
preg_match_all("/(graboard)='(.+?)'/i", $mystring, $matches);
print_r($matches);
将产生:
Array
(
[0] => Array
(
[0] => graboard='KERALA'
[1] => graboard='MG'
)
[1] => Array
(
[0] => graboard
[1] => graboard
)
[2] => Array
(
[0] => KERALA
[1] => MG
)
)
因此,您可以使用count($matches[1])
——然而,可能需要修改此正则表达式以满足您的需要,但这只是一个基本示例。
只需使用preg_match_all()
:
// The string.
$mystring="|graboard='KERALA'||graboarded='KUSAT'||graboard='MG'";
// The `preg_match_all()`.
preg_match_all('/graboard/is', $mystring, $matches);
// Echo the count of `$matches` generated by `preg_match_all()`.
echo count($matches[0]);
// Dumping the content of `$matches` for verification.
echo '<pre>';
print_r($matches);
echo '</pre>';