PHP check occurences of a character inside a string
I got strings like these:
car开发者_开发技巧-122-334
bike-123
boat
I want to know how many "hyphens" in the string. if there is 2 hyphens return 2, if 0 hyphen return 0...
From php.net: substr_count
int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
For example:
$text = "car-122-334";
echo substr_count ($text, '-'); // --> 2
use the substr_count
method in PHP. No need for regex.
echo substr_count($text, '-');
http://www.php.net/manual/en/function.substr-count.php
See http://www.php.net/manual/en/function.substr-count.php and http://php.net/manual/fr/function.preg-match.php.
substr_count will give you what you expect $count = substr_count($str, "-");
精彩评论