php实现获取文章内容第一张图片的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了php实现获取文章内容第一张图片的方法。分享给大家供大家参考。具体分析如下:

采用php获取文章内容的第一张图片方法非常的简单,我们最常用的是使用正则了,感兴趣的朋友可以参考一下下面这段代码。

以下是关于选取文章中第一张图片的代码:

$obj=M("News");
$info=$obj->where('id=1')->find();
//方法1*********
$soContent = $info['content'];
$soImages = '~<img [^>]* />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$thePics[0][0],$match);
dump($thePics);
if( $allPics> 0 ){
  echo "<img src='".$match[1]."' title='".$match[1]."'>";//获取的图片名称
}
else {
  echo "没有图片";
}
//**************
$soContent = $info['content'];
$soImages = '~<img [^>]* />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
dump($thePics);
if( $allPics> 0 ){
  echo $thePics[0][0]; //获取的整个Img属性
} else {
  echo "没有图片";
}
//**************
$soImages = '~<img [^>]* />~';
$str=$info['content'];
preg_match_all($soImages,$str,$ereg);//正则表达式把图片的整个都获取出来了
$img=$ereg[0][0];//图片
$p="#src=('|\")(.*)('|\")#isU";//正则表达式
preg_match_all ($p, $img, $img1);
  $img_path =$img1[2][0];//获取第一张图片路径
if(!$img_path){
  $img_path="images/nopic.jpg";
} //如果新闻中不存在图片,用默认的nopic.jpg替换 */
echo $img_path;
//*************88
$str=$info['content'];
preg_match_all("/<img.*\>/isU",$str,$ereg);//正则表达式把图片的整个都获取出来了
$img=$ereg[0][0];//图片
$p="#src=('|\")(.*)('|\")#isU";//正则表达式
preg_match_all ($p, $img, $img1);
  $img_path =$img1[2][0];//获取第一张图片路径
if(!$img_path){
  $img_path="images/nopic.jpg";
} //如果新闻中不存在图片,用默认的nopic.jpg替换 */
echo $img_path;

php获取文章html内容第一张图片地址

php实现获取文章html内容第一张图片地址,示例采用正则表达式实现,代码仅供参考。也可以稍作修改即可获取文章内容中全部图片地址,具体业务具体扩展。

// 注意这个变量中img标签末尾的细节变化
$str='<center>
    <img src="/zb_users/upload/202003/nozdxjlbrob.jpeg">
    <img src="/zb_users/upload/202003/tfztjixl0zt.jpeg" >
    <img src="/zb_users/upload/202003/2cye0xkb405.jpeg"/>
    <img src="/zb_users/upload/202003/oqle4a2pemz.jpeg" />
   </center>';
echo get_html_first_imgurl($str);
exit;
/**
 * 获取文章内容html中第一张图片地址
 */
function get_html_first_imgurl($html){
  $pattern = '~]*[\s]?[\/]?[\s]?>~';
  preg_match_all($pattern, $html, $matches);//正则表达式把图片的整个都获取出来了
  $img_arr = $matches[0];//全部图片数组
  $first_img_url = "";
  if (!empty($img_arr)) {
    $first_img = $img_arr[0];
    $p="#src=('|\")(.*)('|\")#isU";//正则表达式
    preg_match_all ($p, $first_img, $img_val);
    if(isset($img_val[2][0])){
      $first_img_url = $img_val[2][0]; //获取第一张图片地址
    }
  }
  return $first_img_url;
}

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

基于PHP RSA密文过长加密解密 越过1024的解决方法

如下所示: <?php namespace helpers; class OpensslRSA{ //echo $private_key 私钥; public...

PHP简单选择排序(Simple Selection Sort)算法学习

本文实例为大家分享了PHP简单选择排序的具体代码,供大家参考,具体内容如下 基本思想: 通过 n - i 次关键字间的比较,从 n - i + 1 个记录中选出关键字最小的记录,并和第...

深入eAccelerator与memcached的区别详解

eAccelerator和memcached,是目前较为主流的两个可使用在PHP之中的缓存加速工具.eAccelerator专门为PHP开发,而memcached不仅仅用在PHP之中,其...

php使用Jpgraph绘制3D饼状图的方法

php使用Jpgraph绘制3D饼状图的方法

本文实例讲述了php使用Jpgraph绘制3D饼状图的方法。分享给大家供大家参考。具体实现方法如下: <?php include ("src/jpgraph.php"...

实现php删除链表中重复的结点

删除链表中重复的结点: 定义两个指针pre和current 两个指针同时往后移动,current指针如果与后一个结点值相同,就独自往前走直到没有相等的 pre指针next直接指向cur...