从字符串中获取并保存data-uri

时间:2014-08-26 06:54:43

标签: php replace preg-replace fetch

我试图仅从字符串中获取data-uri。

PHP

$result = '<div class="search-hit__image" data-uri="http://foobar.com/s/resources/images/70474/small/foo/bar.jpg">
<div class="search-hit__product">Foobar
</div>
</div>';

我如何使用php使用preg_replace(或其他东西)来获取数据-uri?

我希望结果是:

$result = 'http://foobar.com/s/resources/images/70474/small/foo/bar.jpg';

1 个答案:

答案 0 :(得分:0)

使用这样一个简单的字符串作为示例,请使用preg_match

preg_match('/data-uri="([^"]+)/', $str, $match);

uri将在$match[1]

如果你的字符串要复杂得多,请使用DomDocument。