How to clear html code in php
php清除html代码的方法:1、通过“strip_tags($str)”去掉HTML及PHP的标记;2、通过htmlspecialchars把html中的标签转换为html实体;3、通过正则表达式过滤css等样式。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
php中去除文字内容中所有html代码
PHP已经为我们提供了很多清除html格式的方法了,下面就让老高介绍一下。
I. strip_tags
strip_tags($str) 去掉 HTML 及 PHP 的标记
语法: string strip_tags(string str);
传回值: 字串
函式种类: 资料处理
内容说明 :
解析:本函式可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。这个函数和 fgetss() 有着相同的功能
例子
echo strip_tags("Hello world!"); # Hello world!
II. htmlspecialchars
这个函数把html中的标签转换为html实体,博客的代码展示就必须使用这个函数,要不贴出来的代码就会被执行了。
预定义的字符是:
& (和号) 成为 & ” (双引号) 成为 ” ‘ (单引号) 成为 ‘ < (小于) 成为 < > (大于) 成为 >
例子
$new = htmlspecialchars("Test", ENT_QUOTES); echo $new; # <a href='test'>Test</a> # 如果需要展现 ,那么浏览器解析HTML的时候会自动将他变为换行 # 但是通过htmlspecialchars就可以让< 变为 '
与htmlspecialchars功能相反的函数是htmlspecialchars_decode,他会把HTML实体转化为字符!
【推荐学习:《PHP视频教程》】
III. 后补函数
PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了
经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。
$descclear = str_replace("\r","",$descclear);//过滤换行 $descclear = str_replace("\n","",$descclear);//过滤换行 $descclear = str_replace("\t","",$descclear);//过滤换行 $descclear = str_replace("\r\n","",$descclear);//过滤换行 $descclear = preg_replace("/\s+/", " ", $descclear);//过滤多余回车 $descclear = preg_replace("/<[ ]+/si","<",$descclear); //过滤<__("<"号后面带空格) $descclear = preg_replace("/<\!--.*?-->/si","",$descclear); //过滤html注释 $descclear = preg_replace("/<(\!.*?)>/si","",$descclear); //过滤DOCTYPE $descclear = preg_replace("/<(\/?html.*?)>/si","",$descclear); //过滤html标签 $descclear = preg_replace("/<(\/?head.*?)>/si","",$descclear); //过滤head标签 $descclear = preg_replace("/<(\/?meta.*?)>/si","",$descclear); //过滤meta标签 $descclear = preg_replace("/<(\/?body.*?)>/si","",$descclear); //过滤body标签 $descclear = preg_replace("/<(\/?link.*?)>/si","",$descclear); //过滤link标签 $descclear = preg_replace("/<(\/?form.*?)>/si","",$descclear); //过滤form标签 $descclear = preg_replace("/cookie/si","COOKIE",$descclear); //过滤COOKIE标签 $descclear = preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$descclear); //过滤applet标签 $descclear = preg_replace("/<(\/?applet.*?)>/si","",$descclear); //过滤applet标签 $descclear = preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$descclear); //过滤style标签 $descclear = preg_replace("/<(\/?style.*?)>/si","",$descclear); //过滤style标签 $descclear = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$descclear); //过滤title标签 $descclear = preg_replace("/<(\/?title.*?)>/si","",$descclear); //过滤title标签 $descclear = preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$descclear); //过滤object标签 $descclear = preg_replace("/<(\/?objec.*?)>/si","",$descclear); //过滤object标签 $descclear = preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$descclear); //过滤noframes标签 $descclear = preg_replace("/<(\/?noframes.*?)>/si","",$descclear); //过滤noframes标签 $descclear = preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$descclear); //过滤frame标签 $descclear = preg_replace("/<(\/?i?frame.*?)>/si","",$descclear); //过滤frame标签 $descclear = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$descclear); //过滤script标签 $descclear = preg_replace("/<(\/?script.*?)>/si","",$descclear); //过滤script标签 $descclear = preg_replace("/javascript/si","Javascript",$descclear); //过滤script标签 $descclear = preg_replace("/vbscript/si","Vbscript",$descclear); //过滤script标签 $descclear = preg_replace("/on([a-z]+)\s*=/si","On\\1=",$descclear); //过滤script标签 $descclear = preg_replace("/
The above is the detailed content of How to clear html code in php. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
