I am trying to use PHP to find and replace from a given string. I suppose this is similar to what you might do in a text editor (regex, or similar).
This is an example initial string:
[quote=Registered_User;0123456]This is a message.[/quote]
The desired output:
[quote=Registered_User pid=0123456]This is a message.[/quote]
I have experimented using preg_replace(), but the issue arises when I want to retain the number element.
I cannot use str_replace() to replace ';' with ' pid=' because there may be instances in the initial string where an isolated semi-colon is found.
Thank you for your time.
We can find an instance where a word, semicolon, and a number come after quote=
and then replace it with the new format:
preg_replace("/quote=(\w+);(\d+)/", "quote=$1 pid=$2", $string);
If the start of the string "Registered_User;0123456]This is a message" does not change you could something like the following.
$string = "[quote=Registered_User;0123456]This is a message.[/quote]";
$beginStr = "[quote=Registered_User pid=";
$string = $beginStr . substr($string,23);
This will find a semicolon preceded by 15 word characters, preceded by "quote=", and change that semicolon into " pid=", however this will only work if the word (Registered_User) is 15 characters.
preg_replace("/(?<=([quote=]{6}(\w{15})));/", " pid=", $string);