Stichy
Stichy

Reputation: 1515

Remove tags and url from img string

I user the CK Editor and want to separate all the image contents when posting. The string is like this when posting:

$string1 = '<h1>Foo bar!</h1> <img src="foo.bar/image.jpg" />';
$string2 = '<p>Hello world</p><br><img src="another.url/image123.jpg" />'

Find images

preg_match_all('/<img[^>]+>/i',$string1, $result1);
preg_match_all('/<img[^>]+>/i',$string2, $result2);

How can I translate the...

<img src="foo.bar/image.jpg" />

To: image.jpg

Any ideas?

Upvotes: 0

Views: 568

Answers (2)

Ananth
Ananth

Reputation: 1550

use this code

<?php
    $doc = new DOMDocument();
    $doc->loadHTML('<img src="another.url/image123.jpg" />');
    $imageTags = $doc->getElementsByTagName('img');

    foreach($imageTags as $tag) {
        echo basename($tag->getAttribute('src'));
    }
?>

Upvotes: 6

blue
blue

Reputation: 1949

preg_match_all('/<img.+?src=".+?([^"\/]+)\"[^>]+>/i',$string1, $result1);

Upvotes: 0

Related Questions