开发者

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 开发者_Go百科on "1"


echo substr_count($url, '/');

See the documentation for more information.


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


http://www.php.net/manual/en/function.substr-count.php


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜