preg_replace n次匹配字符串


preg_replace n times a matching string?

这应该是一个非常简单的问题,但由于某种原因,我无法使其工作,我的框架是PHALCON

$string='GuGuSy';

我想把这个换成

$output = 'AAB';

所以我把它编码为:

 public function graha($planet)
    {
            if(strpos($planet,'Sy')!==false){
                $planet = preg_replace("/Sy/",'B',$planet);
            }
            if(strpos($planet,'Gu')!==false){
                $planet = preg_replace("/Gu/",'A',$planet);
                    //str_replace("Gu",'A',$planet);
            }
      return $planet;
  }

但是我得到的输出不正确,为什么?

我的输出是为字符串是"A"才为什么

问题与Phalcon框架有关,它本身就是

当您返回值时,它似乎只在VOLT模板系统中使用最后一个返回的值。所以你要做的是在函数中回声$planet,以获得在phalcon伏系统中显示的正确值。

$string='GuGuSy';
function graha($planet)
{
$a='';   
if(strpos($planet,'Sy')!==false){
$a= $planet = preg_replace("/Sy/",'B',$planet); // $planet=GuGuB
}
if(strpos($planet,'Gu')!==false){
$a= preg_replace("/Gu/",'A',$planet);
}
return $a;       
}
echo graha($string);