I've got a string containing [center] (with the square brackets) which then needs to be replaced with another string. I'm sure there must be a way of doing this using preg_replace but cant get it working
0
a regular expression would be overkill fo such a task, use 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)
0
Do like this
<?php
$a="abc [dfh]124[center]";
$b=preg_replace ('/\[(.*?)\]/s','[replace]', $a); // this will replace the string with in the square brackets
echo $b;
?>
Hope this will help you
Thank you.
str_replace
– zerkms Dec 7 '18 at 0:50