首页 > Web开发 > 详细

shonc项目中的设计资讯模块 php 字符串操作与正则表达式 strip_tags preg_match

时间:2016-05-10 12:49:32      阅读:211      评论:0      收藏:0      [点我收藏+]

技术分享

问题:当description 内容要求description的值选用资讯内容的前50个汉字。资讯内容可能有图片.

此时需要对输出的内容进行处理

php 正则表达式处理,编辑器输出的内容

只取图片:

preg_match(‘/<\s*img\s+[^>]*?src\s*=\s*(\‘|\")(.*?)\\1[^>]*?\/?\s*>/i‘,$news_detail[‘dnews_content‘],$match);

最简单:只取文字strip_tags ($news_detail[‘dnews_content‘]);

去除所有空格和html标签(包括换行 空格 &nbsp;)

//正则表达式去除所有空格和html标签(包括换行 空格  )

preg_replace("/(\s|\&nbsp\;| |\xc2\xa0)/", "", strip_tags($str));

导读:在wap开发中,获取文章内容的时候,里面的图片都设置宽和高,这样在手机里就不会等比缩小,那怎么用php代码过滤img的宽高呢?

只匹配中文没有英文和标点符号

$htmldescription = preg_replace("/[^\x{4e00}-\x{9fa5}]/iu", "", strip_tags($news_detail[‘dnews_content‘]));

 

php preg_replace过滤img宽高

<?php 
$str=‘<img  border="0"  src="http://zz.bcty365.com/content/uploadfile/201501/74d31420723044.jpg" width="446" height="280">‘; 
echo content_strip($str); 
function content_strip($content) { 
   $content = preg_replace(‘/<img[^>]*src=[\‘"]?([^>\‘"\s]*)[\‘"]?[^>]*>/ie‘, "wap_img(‘$1‘)", $content); 
   return $content; 
} 
function wap_img($url) { 
     return ‘<img src="‘.$url.‘" width="100%">‘; 
} 
?> 

 

shonc项目中的设计资讯模块 php 字符串操作与正则表达式 strip_tags preg_match

原文:http://www.cnblogs.com/lemonphp/p/5477214.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!