PHP获取编辑器内第一张图片

来源:互联网 发布:mysql可视化工具下载 编辑:程序博客网 时间:2024/04/29 00:20

首先讲一下获取编辑器中的图片的原理:

使用代码找到存储 编辑器内容的 字段。然后取出字段  使用正则匹配。取出相应地址,以下为大家提供几种方式。


$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 "没有图片";}//方法2**************$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 "没有图片";}//方法3**************$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;//方法4*************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;


原创粉丝点击