PHP移除指定HTML标签方法总结
在php中我们最常用的指定HTML标签可以直接使用strip_tags函数来替换了,利用它可以过滤所有的html标签哦,下面我来给大家介绍除了此函数之外的其它办法。
有时候我们需要把html标签页存到数据库里,但是有些场合却需要拿无html标签的纯数据,这个时候就要对带html标签的数据进行处理,把html标签都去掉。平时用 htmlspecialchars() 来过滤html,但是把html的字符转义了,最后显示出来的就是html源代码,利用strip_tags()就可以把html标签去除掉。
PHP默认的函数有移除指定html标签,名称为strip_tags,在某些场合非常有用。
strip_tags
strip_tags — Strip HTML and PHP tags from a string
string strip_tags ( string str [, string allowable_tags] )
弊端 :
这个函数只能保留想要的html标签,就是参数string allowable_tags。
这个函数的参数allowable_tags的其他的用法。
代码如下 | 复制代码 |
![]() |
如果想去掉的html的指定标签。那么这个函数就不能满足需求了。
于是乎我用到了这个函数。
代码如下 | 复制代码 |
function strip_only_tags($str, $tags, $stripContent = FALSE) { |
参数说明
$str — 是指需要过滤的一段字符串,比如div、p、em、img等html标签。
$tags — 是指想要移除指定的html标签,比如a、img、p等。
$stripContent = FALSE — 移除标签内的内容,比如将整个链接删除等,默认为False,即不删除标签内的内容。
使用说明
代码如下 | 复制代码 |
$source=' '; $target = strip_only_tags($source, array('a','em')); //target results // ![]() |
其它办法
代码如下 | 复制代码 |
|
一个自定义的函数
/
代码如下 | 复制代码 |
** * 取出html标签 * * @access public * @param string str * @return string * */ function deletehtml($str) { $str = trim($str); //清除字符串两边的空格 $str = strip_tags($str," "); //利用php自带的函数清除html格式。保留P标签 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
