背景:Kindeditor内容保存在数据库中的类型是text,包含文字和HTML标签。

需求:显示内容的前50个字(纯文字内容)

方法:将字段查出去除标签,截取前50

 import java.util.regex.Matcher;
import java.util.regex.Pattern; public class StrUtils { private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
private static final String regEx_img = "<img\\s*([^>]*)\\s*src=\\\"(.*?)\\\"\\s*([^>]*)>";// 定义image标签的正则表达式
private static final String regEx_emoji = "[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\ud83e\\udd00-\\ud83e\\udfff]|[\\u2600-\\u27ff]";// 定义表情标签的正则表达式
private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
private static final String regEx_special = "\\&[a-zA-Z]{1,10};";//定义特殊字符 public static String delHTMLTag(String htmlStr) { // 过滤script标签
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤style标签
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤image标签
Pattern p_img = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
Matcher m_img = p_img.matcher(htmlStr);
htmlStr = m_img.replaceAll(""); // 过滤emoji标签
Pattern p_emoji = Pattern.compile(regEx_emoji, Pattern.CASE_INSENSITIVE);
Matcher m_emoji = p_emoji.matcher(htmlStr);
htmlStr = m_emoji.replaceAll(""); // 过滤html标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤空格回车标签
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤特殊字符
Pattern p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
Matcher m_special = p_special.matcher(htmlStr);
htmlStr = m_special.replaceAll(""); return htmlStr.trim(); // 返回文本字符串
} public static String getTextFromHtml(String htmlStr){
htmlStr = delHTMLTag(htmlStr);
htmlStr = htmlStr.replaceAll(" ", "");
if (htmlStr.length()>50){
htmlStr = htmlStr.substring(0,50);
}
return htmlStr;
} }

去除字符串中的HTML标签的更多相关文章

  1. js去除字符串中所有html标签及&nbsp符号

    近日在做项目的时候,经常会在页面上处理一些数据.结果发现自己js掌握的并不是很好.那就在这里记录js的点点滴滴吧. 1. 去除字符串中的 html 标签 function delHtmlTag(str ...

  2. 正则去除字符串中的html标签,但不去除<br>标签

    一.去除html标签 filterHTMLTag(msg) { var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag msg ...

  3. php去除字符串中的HTML标签

    php自带的函数可以去除/删除字符串中的HTML标签/代码. strip_tags(string,allow):函数剥去 HTML.XML 以及 PHP 的标签. 参数:string,必填,规定要检查 ...

  4. (ASP.NET )去除字符串中的HTML标签

    string strDoContent = "执行增加<a href="/AdminCX/Admin_CompanyDetail.aspx?CompanyGuid=cd8e1 ...

  5. js去除字符串中的标签

    var str="<p>js去除字符串中的标签</p>"; var result=str.replace(/<.*?>/ig,"&qu ...

  6. java 去html标签,去除字符串中的空格,回车,换行符,制表符

    public static String getonerow(String allLine,String myfind)     {                           Pattern ...

  7. 正则匹配去掉字符串中的html标签

    1.得到超链接中的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"&quo ...

  8. 147-PHP strip_tags函数,剥去字符串中的 HTML 标签(一)

    <?php $html=<<<HTM <title>PHP输出HTML代码</title> <body> <a href=#>转 ...

  9. 去除字符串中的html标记及标记中的内容

    去除字符串中的html标记及标记中的内容 --1.创建函数 create function [dbo].[clearhtml] (@maco varchar(8000)) returns varcha ...

随机推荐

  1. python模块学习之HTMLTestRunner模块生成HTML测试报告

    #!/usr/bin/env python #-*- coding:utf-8 -*- from HTMLTestRunner import HTMLTestRunner import time im ...

  2. PHP中输出字符串(echo,print,printf,print_r和var_dump)的区别【转载】

    php中常见的输出语句 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. prin ...

  3. nginx+tomcat负载均衡实验

    导言: 本次实验,tomcat就直接使用录原生的主页,只是简单修改主页识别主机,nginx也是直接在欢迎页上面修改的,直接实现负载均衡. 主机1:192.168.100.156 nginx+tomca ...

  4. vmware安装minimal centos报错/etc/rc5.d/s99local : line:25 : eject : command not found

    今天在用centos mini 版的时候创建虚拟机出现错误提示:vmware安装minimal centos报错/etc/rc5.d/s99local : line:25 : eject : comm ...

  5. html-body标签中相关标签 02

    今日主要内容: 列表标签 <ul>.<ol>.<dl> 表格标签 <table> 表单标签 <fom> 一.列表标签 列表标签分为三种. 1 ...

  6. spark复习总结01

    1.MapReduce和spark的对比 MapReduce Spark 数据存储结构:磁盘hdfs文件系统的split 使用内存构建弹性分布式数据集RDD,对数据进行运算和cache 编程范式:Ma ...

  7. 查看hive版本号

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sheismylife/article/details/33378243 hive没有提供hive - ...

  8. im开发总结:netty的使用

    最近公司在做一个im群聊的开发,技术使用得非常多,各种代码封装得也是十分优美,使用到了netty,zookeeper,redis,线程池·,mongdb,lua,等系列的技术 netty是对nio的一 ...

  9. C 编译器的“贪心法”

    C语言中有单字符符号和多字符符号之分,那么,当C编译器读入一个字符‘/’后又跟了一个字符‘*’,那么编译器就必须做出判断:是将其作为两个分别的符号对待,还是合起来作为一个符号对待.C语言对这个问题的解 ...

  10. handsontable 随记

    handsontable 怎么样获取合并之后的cell信息,如下 handsontable .getPlugin('mergeCells').mergedCellsCollection 看了他的源代码 ...