I want to replace identical <IMG>
tags in a string with an array with different values.
So IMG, IMG will be:
<IMG src="data:image/jpg;base64,/9j/4AA ....etc> , <IMG src="data:image/png;base64,/9j/4AA ....etc >
Since the pattern has to be mixed with preg_replace()
, and because preg_replace_all
doesn't exist, i have to use the function preg_replace_callback()
.
I have PHP version 5.2 so i have to use a create_function()
within preg_replace_callback()
.
Does anybody have an idea how preg_replace_callback()
has to look like in this situation?
I cannot understand how create_function
works. Do i have to make a loop or something?
My code with preg_replace()
: This changes all IMG tags with the first picture of the array. But i need loop through all the IMG tags with preg_replace_callback()
/create_function()
and replace with different pictures of the array.
$artikelinhoud = $simpleXml->StandaardOplossing->attributes()->ArtikelInhoud;
$arrayImages = array();
preg_match_all('<!\[(CDATA)\[\s*(.*?)\s*\]\]>', $artikelinhoud, $arrayImages);
$images = array();
foreach ($arrayImages[2] as $key => $image) {
$images[$key] = 'src="data:image/jpg;base64,' . $image . '"';
}
$imagesOld = array();
$imagesOld[] = '/type="(.*?)"/';
$artikelinhoud = preg_replace($imagesOld, $images , $artikelinhoud);
Variable $imagesold
is always the same. And $images
is an array with different values to put between tags.
The array $images
looks like:
[0]=> string(26464) "src="data:image/jpg;base64,/9j/4A...etc
[1]=> string(23464) "src="data:image/jpg;base64,/9j/4A....etc
Greetz, Eric