/    Sign up×
Community /Pin to ProfileBookmark

replace all images with text

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″ />[/CODE]

And I want result:

[CODE]<p>Cat on roof.</p>
<p>Kittens.</p>
<p>Black cat.</p>[/CODE]

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);
}
[/code]

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>>[/CODE]

Could somebody help me fix this please?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Helleshtern spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...