0

Hi there.

Is there any way that can count the number of slashes in a string?

For example: abs/frg/grgr/rgr/

and,the number of slashes can be obtained?

Thanks for any solutions provided.

3
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by lcyew
0

This will give you the number of slashes in the string $str -

$str = "sdfsd/frg/grgr/rgr/";
$arr = explode('/',$str);
echo $slashCount =  count($arr) -1;
0

yes as kkeith29 says substr_count() is the better choice. it can like below -

$text = "sd/sdasd/dsda/";
echo substr_count($text, '/');

It will be faster than explode()

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.