PHP preg_match_all用于获取带有?w =的图像

时间:2013-11-30 13:32:53

标签: php preg-match-all

我正在尝试使用preg_match_all获取图像,但是无法在此处获取它是我的代码。我的问题,我有.img ext和一些.jpg的wome图像?w = 655& h = 357,我不需要.img但需要所有其他有效的图像吗?w = 655& h = 357 in last或者只是.jpg或.png

$post ='
<img width="1" height="1" src="http://pi.feedsportal.com/r/180265248066/u/49/f/648326/c/35070/s/34410e29/a2t.img" border="0"/></br>
    <img width="1" height="1" src="http://9to5mac.files.wordpress.com/2013/11/screen-shot-2013-11-29-at-5-17-15-pm.png?w=655&#038;h=357" border="0"/></br>
    <img src="http://images.macrumors.com/article-new/2013/11/mlb.png" alt="MLB" title="mlb.png" width="175" height="175" class="alignright"/></br>
 ';

function catch_that_image($post) {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all("<img.+?src=[\"']([^\"]*\.(gif|jpg|jpeg|png).*)[\"'].+?>", $post, $matches);
  $first_img = $matches [1] [0];

  return $first_img;

}
echo catch_that_image($post);

输出

http://images.macrumors.com/article-new/2013/11/mlb.png" alt="MLB" title="mlb.png" width="175" height="175" class="alignright

我只需要url直到.png

由于

1 个答案:

答案 0 :(得分:1)

不要使用正则表达式来解析HTML。请改用DOM解析器:

$dom = new DOMDocument;
$dom->loadHTML($html);

foreach ($dom->getElementsByTagName('img') as $image) {
    $src =  $image->getAttribute('src');
    $extension = pathinfo($src, PATHINFO_EXTENSION);
    if ($extension !== 'img') {
        echo $src . PHP_EOL;
    }
}

Online demo.