Ray S.
Ray S.

Reputation: 1200

Replace All HTML img tags with text inside their ALT

for example:

INPUT:

<img "img1.gif" alt="Donkey"><BR>
<img "img2.gif" alt="Horse"><BR>
<img "img3.gif" alt="Orangutan"><BR>

OUTPUT

Donkey
Horse
Orangutan

looked around for this but no cigar. Any ideas? thnx!

Upvotes: 0

Views: 2661

Answers (3)

Mihai Iorga
Mihai Iorga

Reputation: 39704

for a quick solution use preg_replace()

$text = '<img img1.gif" alt="Donkey"><BR>
<img img2.gif" alt="Horse"><BR>
<img img3.gif" alt="Orangutan"><BR>';

$replace = preg_replace('/<img(.*)alt="(.*)"\>/', "$2", $text);
echo $replace;

Upvotes: 3

Ja͢ck
Ja͢ck

Reputation: 173522

Feeling generous today, here's da c0dez:

$html =<<<EOS
<img "img1.gif" alt="Donkey"><BR>
<img "img2.gif" alt="Horse"><BR>
<img "img3.gif" alt="Orangutan"><BR>
EOS;

$d = new DOMDocument;
$d->loadHTML($html);
foreach ($d->getElementsByTagName('img') as $img) {
    echo $img->getAttribute('alt'), "\n";
}

Upvotes: 3

ipoga
ipoga

Reputation: 394

You can't have looked around a lot - look at this quite similar question:

How to extract img src, title and alt from html using php?

Upvotes: 0

Related Questions