如何使用php计算字符串中的破折号( - )?

时间:2016-03-09 12:41:50

标签: php

如何使用php在字符串中仅计算( - )?

通常我通常使用下面的代码来计算使用php

的字符串中的所有字符长度
$count = mb_strlen( $string );

但是我想知道如何才能使用php计算字符串中的短划线(-)

EG:

$string = "1-2-3-4-5-";

计数后的短划线(-)结果将为5

1 个答案:

答案 0 :(得分:3)

最简单的是使用函数substr_count()

$str = 'some-class-of-css';
echo substr_count($str, '-'); //3