X10nD
X10nD

Reputation: 22030

Count number of times a character appears in a url using PHP

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"

Upvotes: 0

Views: 413

Answers (4)

fabrik
fabrik

Reputation: 14365

echo substr_count($url, '/');

See the documentation for more information.

Upvotes: 10

user254875486
user254875486

Reputation: 11240

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.

Upvotes: -1

Tomalak
Tomalak

Reputation: 338158

echo strlen($url) - strlen(str_replace("/", "", $url));

Upvotes: 1

Related Questions