检查是否存在并更换

问题描述:

$server['s']['name'] = "§4Hello world i'm §1Mafios_X"; 
// 

$name = $server['s']['name']; 

我如何检查是否存在$name变化$4<font color="red">之前另一$4(或某些$)关闭</font>标签,如果不$name后存在任何其他$,靠近完。检查是否存在并更换

我该如何做到这一点?

+0

我不明白你的问题。如果你的意思是''而不是'$',你可以用preg_match()检查它,如果你想替换它,你可以使用str_replace或preg_replace – Robert

如果我明白你也

if(preg_match('/(§4)/', $name)) // if '§4' exists in $name it will go into conditional block 
{ 
    $name = preg_replace('/(§4)/', '/<font color="red">$1<\/font>/', $str); // we replace it 
} 
else 
{ 
    //it doesn't exists 
    $name.="</font>"; // we close with font tag 
} 

Basicly它是所有关于preg_replace('/(§4)/', '/<font color="red">$1<\/font>/', $str);这将改变每个§4<font color="red">§4<\/font>