Home > Backend Development > PHP Problem > How to clear html, spaces, newlines, and extract plain text in php

How to clear html, spaces, newlines, and extract plain text in php

coldplay.xixi
Release: 2023-03-05 12:06:02
Original
2943 people have browsed it

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)].

How to clear html, spaces, newlines, and extract plain text in php

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); //返回字符串
}
Copy after login

Call method

DeleteHtml($str);
Copy after login

$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);
Copy after login

Remove all blank lines, including internal And head and tail:

$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template