0

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

2
  • The way you worded your question will be hard to help you... I think you shouldn't need to use regular expressions, maybe you just need to use DOM and XPath for that... :) Commented Nov 8, 2013 at 8:42
  • yes maybe, but i am almost at a solution. The string is a big html string with a few times IMG tags in it. The only problem now is every image tag in the string is replaced with the first image of the image array. And not with different images. So i will be happy with the golden tip :-) Commented Nov 8, 2013 at 8:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.