1

我有一个这样的html模板:

    <div class="cont">
    <div class="...">
    <p>...<p>
    <img alt="" class="popup" src="DESIRED IMAGE LINK" style="..." /></p><p>...</p>
    ....

我想在“”标签中提取“DESIRED IMAGE LINK”,目前我正在使用这个:

$pattern = '<div class="cont">.*?src=["\']?([^"\']?.*?(png|jpg|jpeg|gif))["\']?/i';
if (preg_match($pattern, $content, $image))
     .....

但它不起作用,错误是:

    warning: preg_match() [function.preg-match]: Unknown modifier '.' 

我该如何解决?谢谢

4

3 回答 3

3

答案是,不要使用正则表达式。

$contents = <<<EOS
<div class="cont">
    <div class="...">
    <p>...<p>
    <img alt="" class="popup" src="DESIRED IMAGE LINK" style="..." /></p><p>...</p>
EOS;

$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($contents);
libxml_clear_errors();

$xp = new DOMXPath($doc);

// get first image inside div.cont
foreach($xp->query('//div[@class="cont"]//img[1]') as $node) {
        // output the src attribute
        echo $node->getAttribute('src'), PHP_EOL;
}

参见:DOMDocument DOMXPath

于 2012-12-18T17:01:27.597 回答
1

If you're planning on parsing html try using DOM with xpath.

于 2012-12-18T17:00:13.807 回答
0

$pattern = '/<div class="cont">.*?src=["\']?([^"\']?.*?(png|jpg|jpeg|gif))["\']?/i

您缺少前导分隔符/

于 2012-12-18T16:58:39.677 回答