我试图找到一种方法来计算所有"','"我的sql预查询中的符号集。现在我正在做的是:
$i=0;
while ((strpbrk($string, "'")) !== false)
{
$string= strpbrk($string, "'");
$string= substr($string, 1);
$i++;
}
$i = $i/2;
echo $i;
这是有效的,直到我在$ string中得到一个引用。 有没有办法计算"''""喜欢preg-match,但我发现它只找到字符串中的第一个或最后一个元素,而不是所有元素..
答案 0 :(得分:3)
更简单的方法是使用explode:
<?php
$string = 'A word, another word, and finally, the end';
$find = ',';
$find_symbol = explode($find, $string);
$total_find = count($find_symbol);
?>
另一种更简单的方式是substr_count:
echo substr_count($string, ',');
但这取决于你需要什么。
干杯。