tags:

views:

50

answers:

4

Hello,

I want to count the number of times "/" appears in this url

Here is my code

$url = "http://www.google.com/images/srpr/nav_logo14.png";
$url_arr = eregi(".",$url);
echo count($url_arr);

It displays on "1"

Appreciate all help

Thanks Jean

+1  A: 
echo strlen($url) - strlen(str_replace("/", "", $url));
Tomalak
+1 for interesting solution =)
Andreas Bonini
A: 

You could use explode() and count():

$url = "http://www.google.com/images/srpr/nav_logo14.png";
$url_arr = explode(".", $url);
echo count($url_arr);

The reason eregi() returns 1 is that eregi() returns the length of the matched string.

Lex
If you check my question, this is what I have used, it gives me "1"
Jean
You did not use explode(), you used eregi()
Lex
+10  A: 
echo substr_count($url, '/');

See the documentation for more information.

fabrik
+1 for simple solution
Andreas Bonini
+1 snap, deleting my answer as it's identical
nathan
+1 better than mine. :-)
Tomalak
Great it worked.....thanks... pls wait 5 mins for me to accept the answer...stackoverflow I got to wait 5 mins......
Jean
@Jean: are you sure you didn't do something like `$url_arr = substr_count($url, '/'); echo count($url_arr);`? There is no `count()` with this solution.
Andreas Bonini
Thanks, Andreas :)
fabrik
+1 nice solution.
Brad F Jacobs