0

I'm new to php and and i need help with something. I need to divide the image and the content from this variable. It has a image and the description.

$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! <a href="/search?q=quote" class="pintag" title="#quote search Pinterest">#quote</a>";

I know this is something simple but please help me. I need the image in a variable and the content with the link in another..

Thanks.

Supun
  • 121
  • 1
  • 1
  • 9

1 Answers1

0

As @Barmar says, you should use a DOM parsing library.

You may find very useful this: http://simplehtmldom.sourceforge.net

Its use is similar to a jQuery parsing, and you only have to read doc to know how to use it (very good examples => http://simplehtmldom.sourceforge.net/manual.htm)

Example:

$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! <a href="/search?q=quote" class="pintag" title="#quote search Pinterest">#quote</a>";

$html = str_get_html($content);

$image = $html->find('img');
$links = $html->find('a');

And you'll have what you want in $image and $links, :D

Federico J.
  • 15,388
  • 6
  • 32
  • 51
  • SimpleHtmlDom is a slow memory hog with a rather poor codebase. Consider one of http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php/3577662#3577662 – Gordon Aug 05 '13 at 06:11
  • Interesting, I'll check it out them, :D. I answered SimpleHtmlDom because I've used it and it worked, with no memory problem (just parsing small pages, thought), and because I didn't know about the other. Thanks Gordon for pointing more, XD – Federico J. Aug 05 '13 at 06:20