利用php的preg_replace函数对html中的标记进行替换。
1
function
html2text(
$str
){
2 $str = preg_replace ( " /<style .*?<\/style>/is " , "" , $str ); $str = preg_replace ( " /<script .*?<\/script>/is " , "" , $str );
3 $str = preg_replace ( " /<br \s*\/?\/>/i " , " \n " , $str );
4 $str = preg_replace ( " /<\/?p>/i " , " \n\n " , $str );
5 $str = preg_replace ( " /<\/?td>/i " , " \n " , $str );
6 $str = preg_replace ( " /<\/?div>/i " , " \n " , $str );
7 $str = preg_replace ( " /<\/?blockquote>/i " , " \n " , $str );
8 $str = preg_replace ( " /<\/?li>/i " , " \n " , $str );
9 $str = preg_replace ( " /\ \;/i " , " " , $str );
10 $str = preg_replace ( " /\ /i " , " " , $str );
11 $str = preg_replace ( " /\&\;/i " , " & " , $str );
12 $str = preg_replace ( " /\&/i " , " & " , $str );
13 $str = preg_replace ( " /\<\;/i " , " < " , $str );
14 $str = preg_replace ( " /\</i " , " < " , $str );
15 $str = preg_replace ( " /\&ldquo\;/i " , ' " ' , $str );
16 $str = preg_replace ( " /\&ldquo/i " , ' " ' , $str );
17 $str = preg_replace ( " /\&lsquo\;/i " , " ' " , $str );
18 $str = preg_replace ( " /\&lsquo/i " , " ' " , $str );
19 $str = preg_replace ( " /\&rsquo\;/i " , " ' " , $str );
20 $str = preg_replace ( " /\&rsquo/i " , " ' " , $str );
21 $str = preg_replace ( " /\>\;/i " , " > " , $str );
22 $str = preg_replace ( " /\>/i " , " > " , $str );
23 $str = preg_replace ( " /\&rdquo\;/i " , ' " ' , $str );
24 $str = preg_replace ( " /\&rdquo/i " , ' " ' , $str );
25 $str = strip_tags ( $str );
26 $str = html_entity_decode ( $str , ENT_QUOTES , $encode );
27 $str = preg_replace ( " /\&\#.*?\;/i " , "" , $str );
28
29 return $str ;
30 }
31
2 $str = preg_replace ( " /<style .*?<\/style>/is " , "" , $str ); $str = preg_replace ( " /<script .*?<\/script>/is " , "" , $str );
3 $str = preg_replace ( " /<br \s*\/?\/>/i " , " \n " , $str );
4 $str = preg_replace ( " /<\/?p>/i " , " \n\n " , $str );
5 $str = preg_replace ( " /<\/?td>/i " , " \n " , $str );
6 $str = preg_replace ( " /<\/?div>/i " , " \n " , $str );
7 $str = preg_replace ( " /<\/?blockquote>/i " , " \n " , $str );
8 $str = preg_replace ( " /<\/?li>/i " , " \n " , $str );
9 $str = preg_replace ( " /\ \;/i " , " " , $str );
10 $str = preg_replace ( " /\ /i " , " " , $str );
11 $str = preg_replace ( " /\&\;/i " , " & " , $str );
12 $str = preg_replace ( " /\&/i " , " & " , $str );
13 $str = preg_replace ( " /\<\;/i " , " < " , $str );
14 $str = preg_replace ( " /\</i " , " < " , $str );
15 $str = preg_replace ( " /\&ldquo\;/i " , ' " ' , $str );
16 $str = preg_replace ( " /\&ldquo/i " , ' " ' , $str );
17 $str = preg_replace ( " /\&lsquo\;/i " , " ' " , $str );
18 $str = preg_replace ( " /\&lsquo/i " , " ' " , $str );
19 $str = preg_replace ( " /\&rsquo\;/i " , " ' " , $str );
20 $str = preg_replace ( " /\&rsquo/i " , " ' " , $str );
21 $str = preg_replace ( " /\>\;/i " , " > " , $str );
22 $str = preg_replace ( " /\>/i " , " > " , $str );
23 $str = preg_replace ( " /\&rdquo\;/i " , ' " ' , $str );
24 $str = preg_replace ( " /\&rdquo/i " , ' " ' , $str );
25 $str = strip_tags ( $str );
26 $str = html_entity_decode ( $str , ENT_QUOTES , $encode );
27 $str = preg_replace ( " /\&\#.*?\;/i " , "" , $str );
28
29 return $str ;
30 }
31