是否有一种方法来计算字符串中的标签并接收一个数字作为结束结果?
$string = 'This is a #0 and #1 works like #2';
$count_hashtags = count('#',$string);
echo $count_hashtags;
//Output: "3"
$string = 'This is a #0 and #1 works like #2'; // Result : 3
$string2= 'foo #bar baz######'; // Result : 1
preg_match_all("/(#'w+)/", $string, $matches);
echo count($matches[0]);
编辑:然后可以把它放在函数
中function countHashTags($string)
{
if(preg_match_all("/(#'w+)/", $string, $matches))
return count($matches[0]);
else
return 0;
}