导航:首页 > 净水问答 > java过滤掉html标签

java过滤掉html标签

发布时间:2021-01-27 10:27:59

A. 在Java截取字符串的时候,如何过滤掉html标签

去除html标签
function
strip_tags($string,
$replace_with_space
=
true)
{
if
($replace_with_space)
{
return
preg_replace('!<[^>]*?>!',
'
',
$string);
}
else
{
return
strip_tags($string);
}
}
截取字符函数(匹配各种编码)
function
truncate($string,
$length
=
80,
$etc
=
'...',
$break_words
=
false,
$middle
=
false){
if
($length
==
0)
return
'';
if
(is_callable('mb_strlen'))
{
if
(mb_detect_encoding($string,
'utf-8,
iso-8859-1')
===
'utf-8')
{
//
$string
has
utf-8
encoding
if
(mb_strlen($string)
>
$length)
{
$length
-=
min($length,
mb_strlen($etc));
if
(!$break_words
&&
!$middle)
{
$string
=
preg_replace('/\s+?(\s+)?$/u',
'',
mb_substr($string,
0,
$length
+
1));
}
if
(!$middle)
{
return
mb_substr($string,
0,
$length)
.
$etc;
}
else
{
return
mb_substr($string,
0,
$length
/
2)
.
$etc
.
mb_substr($string,
-
$length
/
2);
}
}
else
{
return
$string;
}
}
}
//
$string
has
no
utf-8
encoding
if
(strlen($string)
>
$length)
{
$length
-=
min($length,
strlen($etc));
if
(!$break_words
&&
!$middle)
{
$string
=
preg_replace('/\s+?(\s+)?$/',
'',
substr($string,
0,
$length
+
1));
}
if
(!$middle)
{
return
substr($string,
0,
$length)
.
$etc;
}
else
{
return
substr($string,
0,
$length
/
2)
.
$etc
.
substr($string,
-
$length
/
2);
}
}
else
{
return
$string;
}
}
综合就是
$arc=strip_tags($arc);

B. java去掉字段中的html标签

用正则表达式吧,应该比较简单。
或者使用笨点的方法,循环查找'>'符号的内位置,判断下一个字容符是不是'<',如果是,则继续循环,如果不是则是需要留下的文本了,把文本用list保存起来继续循环直到全部字段结束。
最后list里面就是你要留下的文本了

C. java如何利用正则表达式去掉文本中的HTML标签

如下:
publicstaticStringdo_post(Stringurl,List<NameValuePair>name_value_pair)throwsIOException{
Stringbody="{}";
DefaultHttpClienthttpclient=newDefaultHttpClient();
try{
HttpPosthttpost=newHttpPost(url);
httpost.setEntity(newUrlEncodedFormEntity(name_value_pair,StandardCharsets.UTF_8));
HttpResponseresponse=httpclient.execute(httpost);
HttpEntityentity=response.getEntity();
body=EntityUtils.toString(entity);
}finally{
httpclient.getConnectionManager().shutdown();
}
returnbody;
}
publicstaticStringdo_get(Stringurl)throwsClientProtocolException,IOException{
Stringbody="{}";
DefaultHttpClienthttpclient=newDefaultHttpClient();
try{
HttpGethttpget=newHttpGet(url);
HttpResponseresponse=httpclient.execute(httpget);
HttpEntityentity=response.getEntity();
body=EntityUtils.toString(entity);
}finally{
httpclient.getConnectionManager().shutdown();
}
returnbody;
}

D. 用HTMLParser过滤掉html中所有标签,留下标题正文等内容,java

现在的网页,取来title容易,要取到整齐的内源容,就麻烦了。既然是爬虫,又不可能针对每个页面都写一遍。所以,你能解决这问题,是高智商、是值钱的。

<title>和</title>可以认为是标题,用字符串的处理方法即
<content>和</content>不是标准的HTML,不能认为之间的文字就是内容 。虽然<body>和</body>是,可之间的内容也太乱了。

E. java如何去掉字符串中的 html标签

1.去除单个HTML标记
String s="asdfasd<script>asdfsfd</script>1234";
System.out.println(s.replaceAll("<script.*?(?<=/script>)",""));
2.去除所有HTML标记
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLSpirit{ ITjob 远标教育
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签

Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签

Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //过滤html标签

return htmlStr.trim(); //返回文本字符串
}
}

F. 【Java作业向】正则表达式过滤HTML标签

过滤HTML标签的Java正则表达式 (?s)<.*?/?.*?>

按照你的要求编写的用正则表达式过滤HTML标签的Java程序回如下

public class AA {

public String tagFilter(String s){

String regex = "(?s)<.*?/?.*?>";

String ss=s.replaceAll(regex,"");

return ss;

}

public static void main(String[] args) {

String s="<div class="guid time online">测试答 abc</div><span data-url="games/details/" class="guid done">你好13548</span><a href="games/details/" class="guid">15个字母Abc</a><i class="icon-guid"/>";

String result=new AA().tagFilter(s);

System.out.println(result);

}

}

G. java如何利用正则表达式去掉文本中的html标签

正则表达式即可

H. java 如何过滤html代码,只保留中文或英文及基本常用符号

很容易,首先建立一个字符串数组,也就是你需要过滤掉的html标签String[] filterArrays = new String[]{"<html>","</html>","<table>","</table>".....一系列内有关html标签的东西}

当你得到一容个html代码的字符串时你可以循环遍历上面的数组,然后调用String自带的方法replaceAll();
我给你简单的示范一下啊
String str = "dfgdgdfgdgd";//需要过滤的带有HTML标签的代码字符串
for(int i=0;i<filterArrays.length;i++){
if(str.indexOf(filterArrays[i])!=0){
str = str.replaceAll(filterArrays[i],"");//将html标签替换成了空格
}
}

这样就搞定了,主要是你需要在filterArrays中增加你需要过滤的字符串,当然还会有更好的办法,可以不用增加这样的数组,因为出现"<"必然会有">",或者"/>"这样的标签,但是这样做可能会将一些无关的也过滤掉了,总之两种方法都可以,第一种呢我都给你写了例子!祝你成功啊

I. java 如何去除html中的一个指定标签和指定标签里的内容

你好,可以用正则表达式。比如想要去除id为test的div标签及其内容:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Person{
public static void main(String[] args) {
//正则表达专式
Pattern p = Pattern.compile("<div.*id='test'.*</div>");
//测试用的html代码
String str = "<html><body>aa<div id='test'>bb</div></body></html>";
Matcher m = p.matcher(str);
//去除标签属
String result = m.replaceAll("");

System.out.println(result);
}
}

J. java正则表达式过滤html p标签

用JavaScript方法如下,JAVA语言类似:
'你的HTML文本'.replace(/.+>(.+)<.+/,'$1')

阅读全文

与java过滤掉html标签相关的资料

热点内容
医疗废水国家排放标准 浏览:723
石英砂多介质过滤器厂家电话 浏览:61
lsc500树脂 浏览:182
污水处理电费占运行费用比 浏览:164
曝气生物滤池污水厂 浏览:53
天津工业废水治理企业 浏览:44
用什么洗空调滤芯 浏览:257
老式泰山25拖拉机提升器总成 浏览:965
污水泵空气开关跳闸为什么 浏览:235
sbs树脂可剥漆 浏览:961
温江净水过滤 浏览:13
小区饮水机废水能用吗 浏览:889
过滤网厂家58同城 浏览:112
广州医用超纯水器供水设备多少钱 浏览:952
大金空调室内机提升泵 浏览:172
正规滤芯怎么用 浏览:2
树脂包清洗 浏览:506
超滤膜净水器需要清洗一次 浏览:750
汽油滤芯螺丝滑丝了怎么办 浏览:274
临湘污水处理哪里有卖的 浏览:970