PHP中的Preg_replace'shortcodes'

时间:2018-12-07 00:48:55

标签: php

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

2 个答案:

答案 0 :(得分:0)

对于这样的任务,使用正则表达式会显得过大,请使用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)

答案 1 :(得分:0)

这样做

    <?php

$a="abc [dfh]124[center]";

$b=preg_replace ('/\[(.*?)\]/s','[replace]', $a);   // this will replace the string with in the square brackets

echo $b;

?>

希望这对您有帮助

谢谢。

相关问题