使用正则表达式查找图像或iframe

时间:2013-12-19 13:16:00

标签: loops themes

我有以下代码,它会吐出每个帖子的第一张图片:

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image

  }
  echo "<img src=" . $first_img . ">";
}

但是,我还需要捕捉第一个iframe,然后回显最先出的那个。

会是这样的:

  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i' || '/<iframe.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);

我对正则表达式没有经验,所以任何帮助或资源都会很棒:)

0 个答案:

没有答案
相关问题