javahtml标签过滤
1. 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中增加你需要过滤的字符串,当然还会有更好的办法,可以不用增加这样的数组,因为出现"<"必然会有">",或者"/>"这样的标签,但是这样做可能会将一些无关的也过滤掉了,总之两种方法都可以,第一种呢我都给你写了例子!祝你成功啊
2. 在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);
3. java html字符过滤器
这是我看到一个不错的,自己看看吧
import java.util.regex.Pattern;
public class Test
{
public static void main(String[] args)
{
String ww="<html>sss<body>ss</body>ssss</html>";
String ff=html2Text(ww);
System.out.println(ff);
}
public static String html2Text(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script>]*?>[\s\S]*?<\/script>
// }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style>]*?>[\s\S]*?<\/style>
// }
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
textStr = htmlStr;
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}
return textStr;
}
}
4. java正则表达式过滤html p标签
用JavaScript方法如下,JAVA语言类似:
'你的HTML文本'.replace(/.+>(.+)<.+/,'$1')
5. java如何利用正则表达式去掉文本中的html标签
正则表达式即可
6. 过滤所有html标签的几种方法
<!DOCTYPE html>
<html lang="en">
<head>
属<meta charset="UTF-8">
<title>test</title>
<script type="text/javascript">
window.onload = function() {
var oTxt1 = document.getElementById('txt1');
var oTxt2 = document.getElementById('txt2');
var test = document.getElementById('test');
test.onclick = function() {
var reg = /<[^<>]+>/g;
oTxt2.value = oTxt1.value.replace(reg, '');
};
}
</script>
</head>
<body>
<div>
<input type="text" id="txt1">
<input type="text" id="txt2">
</div>
<div><button id="test">测试</button></div>
</body>
</html>
7. java如何去掉字符串中的 html标签
1.去除单个标记
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(); //返回文本字符串
}
}
8. 【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);
}
}
9. 用HTMLParser过滤掉html中所有标签,留下标题正文等内容,java
现在的网页,取title容易,要取到整齐的内容,就麻烦了。既然是爬虫,又内不可能针对每个页面容都写一遍。所以,你能解决这问题,是高智商、是值钱的。
<title>和</title>可以认为是标题,用字符串的处理方法即
<content>和</content>不是标准的HTML,不能认为之间的文字就是内容 。虽然<body>和</body>是,可之间的内容也太乱了。
10. 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);
}
}