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')