用于在博客文章上获取第二张图片的PHP代码

| 我正在使用以下代码来从博客文章中获取图片:
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
  $first_img = \"/images/default.jpg\";
 }
 return $first_img;
}
现在,我需要一些帮助来进行较小的修改。这是我要查找的内容:我希望代码忽略第一个图像,抓取它找到的第二个图像,如果找不到第二个图像,则使用默认图像(后备图像)。     
已邀请:
我是QueryPath项目的忠实拥护者,该项目使您可以像jQuery一样使用HTML文档。将繁琐的工作从此类任务中剔除。试一试,让我知道是否可以帮到您!     
我将第二个答案@David给了您,但是如果您只需要快速而肮脏的修复,则可以执行以下操作:
function catch_that_image() {
  global $post, $posts;
  $first_img = \'\';
  ob_start();
  ob_end_clean();
  $content = preg_replace(\'/<img.+src=[\\\'\"]([^\\\'\"]+)[\\\'\"].*>/i\', \'\', $post->post_content, 1);
  $output = preg_match_all(\'/<img.+src=[\\\'\"]([^\\\'\"]+)[\\\'\"].*>/i\', $content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
  $first_img = \"/images/default.jpg\";
 }
 return $first_img;
}
这里的窍门是使用
preg_replace()
$limit
参数1来删除第一张图像。     

要回复问题请先登录注册