从变量中获取src和内容


Get src and content from a variable

我是php的新手,需要一些帮助。我需要从这个变量中划分图像和内容。它有一个图像和描述。

$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>";

我知道这很简单,但请帮帮我。我需要一个变量中的图像和另一个变量的链接内容。。

谢谢。

正如@Barmar所说,您应该使用DOM解析库。

你可能会发现这很有用:http://simplehtmldom.sourceforge.net

它的使用类似于jQuery解析,您只需要阅读doc就可以知道如何使用它(非常好的例子=>http://simplehtmldom.sourceforge.net/manual.htm)

示例:

$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');

您将在$image和$links中获得所需内容:D