Reputation: 60
I want to replace some strings by other one. I use below codes:
$mc = 'Hello I have these bages: [A-561],[A-123],[A-1],[A-5234]';
$medal = '<img src="1" />';
$bages = preg_replace('/^\[A-[0-9]+\]/i',$medal,$mc);
echo $bages
it prints out this:
Hello I have these bages: [A-561],[A-123],[A-1],[A-5234]
and if i change $mc to
$mc = "[A-561],[A-123],[A-1],[A-5234]";
then prints out:
<img src="1" />,[A-123],[A-1],[A-5234]
I have no idea why it happens like this. i want to change all of them to my replace string.
Upvotes: 0
Views: 41
Reputation: 2143
The ^
in your regex makes the regex only match on the start of a string.
Remove that ^
from the regex:
$bages = preg_replace('/\[A-[0-9]+\]/i',$medal,$mc);
Upvotes: 3