1. 程式人生 > >php 去掉標籤,空格和換行 類似simple_html_dom.php的作用

php 去掉標籤,空格和換行 類似simple_html_dom.php的作用

function DeleteHtml(){

//清除空格和換行
$str = trim($str); //清除字串兩邊的空格
$str = strip_tags($str,""); //利用php自帶的函式清除html格式
$str = preg_replace("/\t/","",$str); //使用正則表示式替換內容,如:空格,換行,並將替換為空。
$str = preg_replace("/\r\n/","",$str);
$str = preg_replace("/\r/","",$str);
$str = preg_replace("/\n/","",$str);
$str = preg_replace("/ /","",$str);
$str = preg_replace("/ /","",$str); //匹配html中的空格
return trim($str);

}

$a = DeleteHtml($str);
echo highlight_string($a);

效果如下(去掉標籤):