Home > Backend Development > PHP Problem > How to remove html code in php

How to remove html code in php

藏色散人
Release: 2023-03-08 13:58:02
Original
3574 people have browsed it

How to remove HTML code in php: 1. Directly remove the tags you want to remove through "function strip($str){}"; 2. Remove HTML tags through the "strip_tags" function; 3. Use the strtr function Just convert specific characters in the string.

How to remove html code in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

Three solutions to deleting HTMl tags in PHP

This article provides a detailed analysis and introduction to the three solutions to deleting HTMl tags in PHP. What you need Friends, please refer to

Method 1:

Directly take out the mark you want to take out

The code is as follows:

<?php
    //取出br标记
    function strip($str)
{
$str=str_replace("<br>","",$str);
//$str=htmlspecialchars($str);
return strip_tags($str);
}
?>
Copy after login

[Recommended: "PHP Video Tutorial》】

Method 2.

There is a strip_tags function in PHP that can easily remove HTML tags.

echo strip_tags(“Hello <b>World</b>”); // 去除 HTML、XML 以及 PHP 的标签。
Copy after login

Non-standard HTML codes can also be removed correctly:

echo strip_tags(“<a href=\”>\”>cftea</a>”); //输出 cftea
Copy after login

In PHP, you can use the strip_tags function to remove HTML tags, see the example below:

The code is as follows:

<?php
$str = ‘www<p>dreamdu</p>.com&#39;;
echo(htmlspecialchars($str).”<br>”);
echo(strip_tags($str));
?>
Copy after login

Method 3.

strtr() function converts specific characters in the string.

Syntax

strtr(string,from,to)
Copy after login

or

strtr(string,array)
Copy after login

Parameters

string1 Required. Specifies the string to be converted.

from Required (unless using an array). Specifies the character to be changed.

to Required (unless using an array). Specifies the character to change to.

array Required (unless from and to are used). An array where the keys are the original characters and the values ​​are the target characters.

Example 1:

The code is as follows:

<?php
echo strtr("Hilla Warld","ia","eo");
?>
Copy after login

Example 2:

The code is as follows:

<?php
$arr = array("Hello" => "Hi", "world" => "earth");
echo strtr("Hello world",$arr);
?>
Copy after login

The above is the detailed content of How to remove html code in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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