以前写项目的时候,每次发布一篇新文章都要上传一张封面图片,感觉有点麻烦,这次写博客为了省力就写了一个方法获取发布内容中的一张图片,没有图片就默认一张.
function get_content_image($str){ preg_match_all("/<img.*\>/isU",$str,$ereg);//正则表达式把图片的整个都获取出来了 if(!empty($ereg[0])){ $img=$ereg[0][0];//图片 $p="#src=('|\")(.*)('|\")#isU";//正则表达式只获取链接地址 preg_match_all ($p, $img, $img1); $img_path =$img1[2];//获取第一张图片路径 if(empty($img_path)){ $img_path="/uploads/default.jpg"; }else{ $img_path =$img1[2][0];//获取第一张图片路径 } }else{ //如果没有图片就默认一张 $img_path="/uploads/default.jpg"; } return $img_path; }
方法写好之后,再发布文章的时候直接调用即可。
$info['images'] = get_content_image($info['content']);
0条评论