学习资料_文档下载_软件应用_程序模板

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

搜索
查看: 1617|回复: 0

php清除html标签,除空格和换行

[复制链接]

186

主题

188

帖子

678

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
678
发表于 2020-9-15 22:10:43 | 显示全部楼层 |阅读模式

方法一,strip_tags()剥去字符串中的 HTML 标签:

  1. <?php
  2. echo strip_tags("Hello <b>world!</b>");
  3. //运行结果:Hello world!
  4. ?>
复制代码

方法二,自定义函数去除

  1. function cutstr_html($string, $sublen)
  2. {
  3.     $string = strip_tags($string);
  4.     $string = preg_replace ('/\n/is', '', $string);
  5.     $string = preg_replace ('/ | /is', '', $string);
  6.     $string = preg_replace ('/ /is', '', $string);

  7.     preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $t_string);
  8.     if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…";
  9.     else $string = join('', array_slice($t_string[0], 0, $sublen));

  10.     return $string;
  11. }
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

HI223.COM 举报邮箱:345306361@qq.com

GMT+8, 2024-5-18 18:10 , Processed in 0.102725 second(s), 18 queries .

Powered by HI223分享社区 2.3

© 2019-2020 Hi223 All Right Reserved

渝公网安备 50022502000358号

渝ICP备14008270号-3