如何从strstr函数php计算值?

时间:2016-06-18 18:16:44

标签: php arrays string strstr

我尝试在符号'@'后获取值并计算它。但是这段代码没有给出正确的结果。如何解决?

$t="Hi, I invite @nina and @nana to come to my party tomorow";
$arr=explode(' ', $t);
foreach($arr as $user ) {
$result=strstr($user, '@');
$total = $count($result);
}
echo $total;
  

结果= 1

     

预期结果= 2

2 个答案:

答案 0 :(得分:3)

为什么不用@?

爆炸它?
$t="Hi, I invite @nina and @nana to come to my party tomorow";
$arr=explode('@', $t);
$total = count($arr)-1;
echo $total;

答案 1 :(得分:2)

试试这段代码。

$t="Hi, I invite @nina and @nana to come to my party tomorow";
$arr=explode(' ', $t);
$total =0;
foreach($arr as $user ) {
$result=strstr($user, '@');
$total = $count($result);
$total++;
}
echo $total;