过滤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)
}
}
去除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)