php method to clear html, spaces, newlines, and extract plain text: 1. Remove blank lines inside the string, the code is [$str = preg_replace('',$str)]; 2. Clear characters For spaces on both sides of the string, the code is [$str = trim($str)].
【Related learning recommendations: php graphic tutorial】
php method to clear html, spaces, newlines, and extract plain 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 string that needs to be cleared
Method 2:
Remove blank lines inside the string:
$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
Remove all blank lines, including internal And head and tail:
$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
Related learning recommendations: php programming (video)
The above is the detailed content of How to clear html, spaces, newlines, and extract plain text in php. For more information, please follow other related articles on the PHP Chinese website!