The thing I’m trying to do is search for all images in document and put them to array. Later I want to replace all ellements from this array with ellements from other array.
For ex. I have 3 images in my document:
[CODE]<img src=”cat1.jpg” alt=”Cat 1″ />
<img src=”cat2.jpg” alt=”Cat 2″ />
<img src=”cat3.jpg” alt=”Cat 3″ />
And I want result:
[CODE]<p>Cat on roof.</p>
<p>Kittens.</p>
<p>Black cat.</p>
This is my actual code:
[code=php]
if ($fp = fopen(‘testfile.php’, ‘r’)) {
$content = ”;
while ($line = fgets($fp, 1024)) {
$content .= $line;
}
}
$img = ‘/<img[^>]+>/i’;
preg_match_all($img, $content, $searchIMG);
$image = Array();
$image[0] = ‘Cat on roof.’;
$image[1] = ‘Kittens.’;
$image[2] = ‘Black cat.’;
$count_array = count($searchIMG[0]);
for($i=0; $i<$count_array; $i++) {
echo preg_replace($searchIMG[0][$i], ‘<p>’.$image[$i].'</p>’, $content);
}
And actual result:
[CODE]<<p>Cat on roof.</p>>
<img src=”cat2.jpg” alt=”Cat 2″ />
<img src=”cat3.jpg” alt=”Cat 3″ />
<img src=”cat1.jpg” alt=”Cat 1″ />
<<p>Kittens.</p>>
<img src=”cat3.jpg” alt=”Cat 3″ />
<img src=”cat1.jpg” alt=”Cat 1″ />
<img src=”cat2.jpg” alt=”Cat 2″ />
<<p>Black cat.</p>>
Could somebody help me fix this please?