首页 > 解决方案 > Preg_replace '短代码'在 php

问题描述

我有一个包含 [center] 的字符串(带有方括号),然后需要用另一个字符串替换。我确定必须有一种方法可以使用 preg_replace 但无法使其正常工作

标签: php

解决方案


一个正则表达式对于这样的任务来说太过分了,使用str_replace

$str="I've got a string containing [center] (with the square brackets)";

$new_str=str_replace('[center]','[left]',$str);

echo $new_str; //I've got a string containing [left] (with the square brackets)

推荐阅读