How to remove div tags in php

藏色散人
Release: 2023-03-10 15:24:01
Original
2697 people have browsed it

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

How to remove div tags in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to remove div tags with PHP? Delete with PHP Three solutions to HTMl tags

Method 1:

Directly take out the tag 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

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

Recommended study:《PHP video tutorial

The above is the detailed content of How to remove div tags 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