php method to remove html, spaces, line breaks, and extract plain text: 1. Clear the spaces on both sides of the string, the code is [$str = trim($str)]; 2. Match the spaces in the html, The code is [$str = preg_replace("/ /","",$str)].
php method to remove html, spaces, line breaks, and extract pure text:
Method 1:
function DeleteHtml($str) { $str = trim($str); //清除字符串两边的空格 $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); //返回字符串 }
Call method
DeleteHtml($str);
$str
is the page character that needs to be cleared String
Method 2:
function DeleteHtml($str) { $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); //返回字符串 }
Method 3:
Remove blank lines inside the string:
$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
Remove all blank lines, including internal and header:
$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
Related learning recommendations:php programming(video)
The above is the detailed content of How to remove html, spaces, line breaks and extract plain text in php. For more information, please follow other related articles on the PHP Chinese website!