0

First post ever here, and i really hope you can help.

I use a database where a large piece of html is stored, now i just need the src part of the image tag.

I already found a thread, but i just doesn't do the trick.

My code:

Original string:

<p><img alt=\"\" src=\"http://domain.nl/cms/ckeditor/filemanager/userfiles/background.png\" style=\"width: 80px; height: 160px;\" /></p>

How i start:

$image = strip_tags($row['information'], '<img>');
echo stripslashes($image); 

This returns:

<img alt="" src="http://domain.nl/cms/ckeditor/filemanager/userfiles/background.png" style="width: 80px; height: 160px;" />

Next step: extract the src part:

preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $image, $matches);

echo $matches ;

This last echo returns:

Array

What is going wrong?

Thanks in advance for your anwser.

1 Answers1

1

Using regexp to solve this kind of problem is a bad idea and will likely lead in unmaintainable and unreliable code. Better us an HTML parser.

Using simplehtmldom you can easily extract the src from the img tag:

foreach($html->find('img') as $element) 
       echo $element->src . '<br/>';
Community
  • 1
  • 1
Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103