How to remove html, spaces, line breaks and extract plain text in php

coldplay.xixi
Release: 2023-03-04 15:04:01
Original
2553 people have browsed it

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

How to remove html, spaces, line breaks and extract plain text in php

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

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

Method 3:

Remove blank lines inside the string:

$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
Copy after login

Remove all blank lines, including internal and header:

$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 remove html, spaces, line breaks 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