I have the following string:
Welcome [firstname] [lastname]
I am trying to use preg_replace() so that [firstname] is replaced with Marcello and [lastname] with Silvestri.
I have only been able to find the way to get the text inside the brackets, but I want to replace the brackets themselves.
$string = "Welcome [firstname] [lastname]";
$patterns = array();
$patterns[0] = '\[firstname\]';
$patterns[1] = '\[lastname\]';
$replacements = array();
$replacements[0] = 'Marcello';
$replacements[1] = 'Silvestri';
echo preg_replace($patterns, $array, $string]);
I also tried with \[(.*?)\] trying to replace everything regardless of the square brackets content, but I get something like [Marcello], while I just want Marcello.
How can I replace the content including the square brackets?
/, like'/\[firstname\]/'Delimiter must not be alphanumeric or backslashafter theParse error: syntax error, unexpected ']' on line 12was fixed. php.net/manual/en/regexp.reference.delimiters.php