PHP检查字符串中字符出现的次数

4

我有这样的字符串:

car-122-334
bike-123
boat

我想知道字符串中有多少个连字符。如果有2个连字符则返回2,如果没有连字符则返回0...

3个回答

7

From php.net: substr_count

int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )

例如:

$text = "car-122-334";
echo substr_count ($text, '-'); // --> 2

6

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接