要利用php获取文章中所有图片中第一张图片出来我们只需要简单的正则表达式即可实现了,下面小编来给大家分享两个实例吧。
首先看一个函数,代码如下:
- function getpic($str_img){
- 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];//获取第一张图片路径
- return $img_path;
- }
- //假如数据库已打开,用$nr获取数据库中的新闻内容
- $nr=$row_news["nr"];
- $aa=getpic($nr_a);
- if(!$aa){$aa="images/nopic.jpg";} //如果新闻中不存在图片,用默认的nopic.jpg替换
再看一个函数相对比较复杂了
在做项目的时候,页面的设计有时会留有文章特色图像的位置,可是有时,这篇文章反而没有上传图片,则在页面中显示的时候则是没有图片,样式上很难看,如果单纯是没有上传图片选用默认图片的时候,有时会引起一些误解,则在考虑是不是先对这个文章图片的问题细化处理,先判断是否有上传的图片,如果有则显示上传的图片,没有则判断内容中是否有图片,有则选取第一张图片作为此处的特色图片,如果连内容中也没有图片的话,则在此处显示默认图片;
以下是关于选取文章中第一张图片的代码,代码如下:
- $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;
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php 获取文章内容的第一张图片实例
波比源码 » php 获取文章内容的第一张图片实例
brand levofloxacin 250mg buy levaquin 250mg
cialis fГјr frauen original tadalafil rezeptfrei sicher kaufen sildenafil generika
buspirone 10mg price order buspirone online cheap buy oxybutynin 2.5mg without prescription
cheap molnunat 200 mg prevacid price buy prevacid 15mg online cheap
revia drug order abilify 30mg online abilify for sale online
cost provera order periactin online cheap buy cyproheptadine 4 mg for sale
purchase lasix online order furosemide 40mg generic plaquenil 200mg for sale
buy cialis 5mg betamethasone 20 gm cheap purchase anafranil sale
aralen generic order chloroquine pills buy olumiant 4mg pill
glycomet cheap buy tadalafil 10mg pills purchase cialis online cheap
clozapine 100mg without prescription cost combivent 100 mcg order dexamethasone 0,0,5 mg generic
omeprazole 20mg cheap professional letter writing services real online casino
order levitra pill order vardenafil 10mg online cheap medrol 16 mg for sale
cheap tadalafil pills levitra or viagra purchase sildenafil
domperidone order online generic sumycin 250mg order generic cyclobenzaprine 15mg
cialis 10mg without prescription oral cialis generic ciprofloxacin
simvastatin 10mg us valacyclovir online buy proscar price
order cefuroxime pill buy ceftin 500mg pills methocarbamol over the counter
trazodone 100mg brand purchase sildenafil online cheap purchase aurogra without prescription
sildenafil next day delivery sildenafil for sale lamotrigine cheap
buy lasix 40mg without prescription purchase doxycycline online order plaquenil 200mg pills
order deltasone 20mg online cheap order minipress 2mg online cheap order mebendazole generic
clarithromycin 250mg without prescription meclizine 25 mg canada meclizine for sale online
naprosyn pills prevacid 15mg brand prevacid 15mg ca
tiotropium bromide order online cost hytrin 1mg hytrin 5mg uk
albuterol 100mcg tablet ciprofloxacin over the counter ciprofloxacin 500mg price
actos over the counter viagra 50mg for sale viagra 100mg usa
order montelukast viagra for sale purchase viagra online cheap
cialis overnight shipping usa gnc ed pills tadalafil for women
ivermectin cost purchase dapsone pills dapsone tablet
essay writing sites azulfidine 500mg us azulfidine 500 mg drug
doxycycline 100mg pill order cleocin 150mg without prescription cleocin ca
order olmesartan online order verapamil 120mg generic purchase divalproex online
temovate order buy buspirone 5mg generic buy cordarone 100mg pills
order lanoxin order digoxin without prescription purchase molnupiravir online cheap
amoxicillin generic buy stromectol 12mg sale buy ivermectin for humans australia
pamelor for sale brand paxil 10mg paroxetine 10mg sale
order indomethacin 50mg pills generic tamsulosin cenforce 50mg usa
buy pepcid 20mg sale buy tacrolimus online mirtazapine 15mg over the counter
buy tadacip 20mg online cheap order generic tadalafil 20mg purchase trimox pill
serophene oral prednisolone 20mg sale order prednisolone 5mg sale
buy absorica sale order deltasone 40mg generic order ampicillin 500mg without prescription
accutane 40mg brand azithromycin drug buy zithromax 500mg online cheap