How can I get preg_replace to perform multiple replacements on the same string with different values at different places?
I actually have a comma separated string of image paths, that I split into multiple <img src="">
tags like below.
$a = /images/us/US01021422717777-%s.jpg,/images/us/US01021422717780-%s.jpg
Then I do:
preg_replace('~\s?([^\s,]+)\s?(?:,|$)~','<img class="gallery" src="$1" data-slide="$1"></div>', $a)
Which gives me:
<img src="/images/us/US01021422717777-%s.jpg" data-slide="/images/us/US01021422717777-%s.jpg">
<img src="/images/us/US01021422717780-%s.jpg" data-slide="/images/us/US01021422717780-%s.jpg">
What can I do to replace the first %s
with m
and the second %s
with %l
, using additional code in the same preg_replace statement itself. Without using preg_callback etc.
So my end results look like:
<img src="/images/us/US01021422717777-m.jpg" data-slide="/images/us/US01021422717777-l.jpg">
<img src="/images/us/US01021422717780-m.jpg" data-slide="/images/us/US01021422717780-l.jpg">
I tried using sprintf
, but it appears that will not work in this situation.
sprintf(preg_replace('~\s?([^\s,]+)\s?(?:,|$)~','<img class="gallery" src="$1" data-slide="$1"></div>', $a),'m','l')
I'm sure you get the idea of what I'm trying to do. Any help on this?