Home  >  Article  >  Backend Development  >  How to remove html code in php

How to remove html code in php

藏色散人
藏色散人Original
2021-03-08 10:09:143470browse

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);
}
?>

[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 的标签。

Non-standard HTML codes can also be removed correctly:

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

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));
?>

Method 3.

strtr() function converts specific characters in the string.

Syntax

strtr(string,from,to)

or

strtr(string,array)

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");
?>

Example 2:

The code is as follows:

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

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!

Statement:
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