【问题标题】:Check if exists and replace检查是否存在并替换
【发布时间】:2013-05-17 12:06:44
【问题描述】:
$server['s']['name'] = "§4Hello world i'm §1Mafios_X";
//

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

如何检查$name 是否存在$4 将其更改为<font color="red"> 并在另一个$4(或一些$)之前关闭</font> 标签,如果不存在任何其他@ 987654328@,$name完成后关闭。

我怎样才能做到这一点?

【问题讨论】:

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

标签: php fonts colors tags


【解决方案1】:

如果我理解你的话

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
}

基本上都是关于preg_replace('/(§4)/', '/&lt;font color="red"&gt;$1&lt;\/font&gt;/', $str); 的,每个§4 都会更改为&lt;font color="red"&gt;§4&lt;\/font&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多