回楼上三位,我是想知道这个正则怎样写. 在php.net上找到这样的方法 function html2txt($document){ $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA ); $text = preg_replace($search, '', $document); return $text; }这个函数可以将所有标签清除,但会连img也清除掉了,如何可以只保留<img />标签,而去掉其他HTML标签,谢谢!
这个函数可以将所有标签清除,但会连img也清除掉了,如何可以只保留<img />标签,而去掉其他HTML标签,谢谢!---------------------------- Replace "<img/>" to "[img/]" before you use the re, and after use the re, replace it back.
在php.net上找到这样的方法
function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
);
$text = preg_replace($search, '', $document);
return $text;
}这个函数可以将所有标签清除,但会连img也清除掉了,如何可以只保留<img />标签,而去掉其他HTML标签,谢谢!
Replace "<img/>" to "[img/]" before you use the re, and after use the re, replace it back.
楼主还顶啊。。陈水给出的这个代码有什么不行吗??
"但不想用PHP的strip_tags,用preg_replace的话,应该怎样写呢?"
就是不想用strip_tags啊.谢谢zeroleonhart,陈水,这两个方法我也有想过...但现我还是想知道这个正则表达式怎样写.
If $str = 'we all know 1<2 and 3>2' .........