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.

Recommended Answers

All 4 Replies

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;

or use substr_count()

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()

It's working!!! Thanks a lot....^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.