PHP开发小技巧①⑥—提取富文本字符串中的文本内容
综述
富文本在我们平常的项目中应用已经很广泛了,并逐渐发展成了一个行业。最近在项目中遇到需要提取富文本字符串中的文本内容,本篇博文就是记述如何重富文本字符串中提取出文本内容,欢迎大家相互学习。
富文本字符样例
<h1 style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-size: 24px; margin: 36px 0px 22px; padding: 0px; line-height: 32px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">一、算法最最基础
</h1>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">1、时间复杂度
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">2、空间复杂度
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">一般最先接触的就是时间复杂度和空间复杂度的学习了,这两个概念以及如何计算,是必须学的,也是必须最先学的,主要有最大复杂度、平均复杂度等,直接通过博客搜索学习即可。
</p>
<h1 style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-size: 24px; margin: 36px 0px 22px; padding: 0px; line-height: 32px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">二、基础数据结构
</h1>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">1、线性表
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">列表(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">链表(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">跳跃表(知道原理,应用,最后自己实现一遍)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">并查集(建议结合刷题学习)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">不用说,链表、列表必须,不过重点是链表。
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">2、栈与队列
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">栈(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">队列(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">优先队列、堆(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">多级反馈队列(原理与应用)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">特别是优先队列,再刷题的时候,还是经常用到的,队列与栈,是最基本的数据结构,必学。可以通过博客来学习。相关文章:
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">三分钟基础知识:什么是栈?
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">二叉堆是什么鬼?
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">【算法与数据结构】堆排序是什么鬼?
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">3、哈希表(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">碰撞解决方法:开放定址法、链地址法、再次哈希法、建立公共溢出区(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">布隆过滤器(原理与应用)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">哈希表相关的,推荐通过博客来学习,推荐文章:
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">Hash冲突之开放地址法
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">4、树
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">二叉树:各种遍历(递归与非递归)(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">哈夫曼树与编码(原理与应用)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">AVL树(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">B 树与 B+ 树(原理与应用)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">前缀树(原理与应用)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">红黑树(原理与应用)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">线段树(原理与应用)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">5、数组
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">树状数组
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">矩阵(必学)
</p>
<p style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">树状数组其实我也没学过
</p>
处理富文本字符串内容
1. 首先通过php的htmlspecialchars_decode()函数把预定义的 HTML 实体 "<"(小于)和 ">"(大于)转换为字符; 2. 其次通过str_replace() 将HTML中的空格换成空; 3. 最后再通过strip_tags()函数剥去字符串中的 HTML、XML 以及 PHP 的标签,即可得到想要的txt纯文本内容。
代码实现
//从数据库获取富文本string
$string = $data["content"];
//把一些预定义的 HTML 实体转换为字符
$html_string = htmlspecialchars_decode($string);
//将空格替换成空
$content = str_replace(" ", "", $html_string);
//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
$contents = strip_tags($content);//返回字符串中的前80字符串长度的字符
$text = mb_substr($contents, 0, 80, "utf-8");//打印输出
var_dump($text);
exit;
PHP开发小技巧①⑥—提取富文本字符串中的文本内容相关推荐
- 【HTML5游戏开发小技巧】RPG情景对话中,令文本逐字输出
以前用javascript实现过令文本逐字输出的效果,今天我来用html5中的canvas实现一下.canvas里的内容可不像<p>那样好操作,首先,你需要懂得一些html5的API才能操 ...
- 移动Web开发小技巧
移动Web开发小技巧 添加到主屏后的标题(IOS) name="apple-mobile-web-app-title" content="标题"> 启用 ...
- 日常安排php,PHP日常开发小技巧
PHP日常开发小技巧 导语:PHP语言中,如果你懂得一些开发技巧,那么对你学PHP,会有很大的帮助.下面的是百分网小编为大家整理的PHP日常开发小技巧,希望对你能有所帮助. PHP批量取得checkb ...
- 小六六平时的开发小技巧二(Nacos在服务配置中心的妙用)
前言 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/bin392328206/six-finger 絮叨 小六六打算开一个系列把自己平时的一些开发经验分享出来和 ...
- 10个JavaScript开发小技巧
"梦想天空"(网名)曾发表一篇博文,为我们介绍了10个JavaScript开发小技巧,现转载于此,供大家学习: 尽管我使用Javascript来做开发有很多年了,但它常有一些让我很 ...
- Android开发小技巧 | 一句命令搞定截屏
-- 简书作者 谢恩铭 转载请注明出处 一句命令搞定截屏 在安卓开发中, 我们很多时候都要用到截屏这个功能. 有时是为了演示, 有时是为了报告问题(比如在Bugzilla, Jira, Redmine ...
- Android 开发小技巧 | 一句命令搞定截屏
-- 作者 谢恩铭 转载请注明出处 一句命令搞定截屏 在安卓开发中, 我们很多时候都要用到截屏这个功能. 有时是为了演示, 有时是为了报告问题(比如在Bugzilla, Jira, Redmine等B ...
- 27个iOS开发小技巧
<span style="word-wrap: normal; word-break: normal; line-height: 1.5em; font-size: 14px; out ...
- Silverlight 游戏开发小技巧:动感小菜单2
Silverlight 游戏开发小技巧:动感小菜单2 动感小菜单其实是想模仿Apple的菜单按钮设计制作,但是画虎不成反类犬,看起来有点别扭,昨天各位园友提了这方面的建议,感觉太硬如果加入动画可能更好 ...
最新文章
- java实验2词法分析程序设计
- restful接口定义的几种方式
- C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现。
- 科大星云诗社动态20211102
- 分布式文件系统之MooseFS----管理优化
- [非旋平衡树]fhq_treap概念及模板,例题:普通平衡树,文艺线段树
- qtableview不选中_如何选中/取消选中QTableView并触发setData()
- NSString,最美的创建方式也最毒
- Shader实例:NGUI制作网格样式血条
- API的理解和使用——有序集合
- RDPwrap: win10 家庭版开通远程桌面服务
- 2021年危险化学品经营单位安全管理人员最新解析及危险化学品经营单位安全管理人员模拟试题
- Ubuntu16.04+智能车+YOLO
- 【Linux杂篇】Windows远程登陆Linux、Linux静态IP配置
- Kibana - KQL语法
- 解决标题党的计算机算法用户app,今日头条算法工程师:做好推荐必须打击标题党...
- 孙子兵法 军形第四(翻译)
- ESP32+TFTLCD实现WiFi天气语音播报(三)
- NumPy简明教程(二、数组1)
- ecshop 实现qq,新浪,淘宝登陆接口
热门文章
- vue - vue中使用西瓜播放器xgplayer
- 怎么更新opengl.dll文件_微信又更新了:群接龙怎么玩?文件如何备份?怎么发高清大视频?...
- linux内核编译时bad register name `%dil'错误
- SVG 的 foreignObject 标签
- svg基础--基本语法与标签
- 接入google 登录 google oauth2
- 给定平面上任意三个点的坐标(x​1​​,y​1​​)、(x​2​​,y​2​​)、(x​3​​,y​3​​),检验它们能否构成三角形
- python 使用精灵代理
- node php v2ex,仿V2EX开源二次元论坛程序+安装教程
- php开源混合模式吗,CSS3混合模式使用详解