Home Backend Development PHP Tutorial PHP removes redundant HTML, Javascript, and Css tags_PHP tutorial

PHP removes redundant HTML, Javascript, and Css tags_PHP tutorial

Jul 13, 2016 pm 05:15 PM
css html php introduce about remove redundant article Label of

本文章来给大家介绍关于各种PHP去除多余的HTML,Javascrit,Css标签 方法与实现程序,大家可进入参考。

1.不保留任何HTML标签,代码会是这样:echo strip_tags($str);  

2. 只保留

一个标签的话,只需要将

字符串写到strip_tags的第二个参数中,代码会是这样:echo strip_tags($str, "

");  

3. 我们要保留

…多个标签,只需要将多个标签用空格分隔后写到strip_tags的第二个参数中,代码会是这样:echo strip_tags($str, "

");

4.保留所有标签,仅仅转义用addslashes(), stripslashes(), htmlspecialchars(), htmlentities(), nl2br() 等函数.

 addslashes(), stripslashes() 一般是入数据库和出库的时候使用,以免变量中存储类似引号这些关键词,这样的话,本来是内容的部分却被数据库识别为标识符来执行,就会引起错误.

 htmlspecialchars() 函数只用来转义少量HTML, &,双引号,大于号和小于号.并不会全部转换成 HTML 所定的 ASCII 转换

 htmlentities() 本函数有点像 htmlspecialchars() 函数,但本函数会将所有 string 的字符都转成 HTML 的特殊字集字符串。不过在转换后阅读网页源代码的方面,会有很多困扰,尤其是网页源代码的中文字会变得不知所云,浏览器上看到的还是正常的。

 

自带函数去除html标记

strip_tags

  去掉 HTML 及 PHP 的标记。

  语法: string strip_tags(string str);

  传回值: 字串

  函式种类: 资料处理

 代码如下 复制代码


$new = htmlspecialchars("Test", ENT_QUOTES); 
echo $new; 

?> 

函式将特殊字元转成 HTML 的字串格式 ( &....; )。最常用到的场合可能就是处理客户留言的留言版了。

  & (和) 转成 &
  " (双引号) 转成 "
  < (小于) 转成 <
  > (大于) 转成 >
  此函式只转换上面的特殊字元,并不会全部转换成 HTML 所定的 ASCII 转换。

 

这里只替换 html,js,css

 代码如下 复制代码

function get_enhtml($string){
$pattern=array ("']*?>.*?'si",// 去掉 javascript
"']*?>.*?'si",// 去掉 HTML 标记
"'<[/!]*?[^<>]*?>'si",//去掉 HTML 标记
"'/i",'', $content);//注释内容 
  $content = preg_replace("/]*-->/i",'', $content);//注释内容      
  $content = preg_replace("/style=.+?['|"]/i",'',$content);//去除样式  
  $content = preg_replace("/class=.+?['|"]/i",'',$content);//去除样式  
  $content = preg_replace("/id=.+?['|"]/i",'',$content);//去除样式     
  $content = preg_replace("/lang=.+?['|"]/i",'',$content);//去除样式      
  $content = preg_replace("/width=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/height=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/border=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/face=.+?['|"]/i",'',$content);//去除样式
     $content = preg_replace("/face=.+?['|"]/",'',$content);
     $content = preg_replace("/face=.+?['|"]/",'',$content);
     $content=str_replace( " ","",$content);
     return $content;
    }

复制代码 function noHTML($content)
    {
     $content = preg_replace("/]*>/i",'', $content);  
  $content = preg_replace("//i", '', $content);   
  $content = preg_replace("/]*>/i",'', $content);  
  $content = preg_replace("/
/i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);  
  $content = preg_replace("//i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);  
  $content = preg_replace("/

/i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);  
  $content = preg_replace("//i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);
  $content = preg_replace("//i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);
  $content = preg_replace("//i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);
  $content = preg_replace("//i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);
  $content = preg_replace("/
/i",'', $content);
  $content = preg_replace("/]*>/i",'', $content);
       $content = preg_replace("//i",'', $content);
  $content = preg_replace("//i",'', $content);//注释内容 
  $content = preg_replace("/]*-->/i",'', $content);//注释内容      
  $content = preg_replace("/style=.+?['|"]/i",'',$content);//去除样式  
  $content = preg_replace("/class=.+?['|"]/i",'',$content);//去除样式  
  $content = preg_replace("/id=.+?['|"]/i",'',$content);//去除样式     
  $content = preg_replace("/lang=.+?['|"]/i",'',$content);//去除样式      
  $content = preg_replace("/width=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/height=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/border=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/face=.+?['|"]/i",'',$content);//去除样式
     $content = preg_replace("/face=.+?['|"]/",'',$content);
     $content = preg_replace("/face=.+?['|"]/",'',$content);
     $content=str_replace( " ","",$content);
     return $content;
    }

http://www.bkjia.com/PHPjc/628786.htmlwww.bkjia.comtrue
http://www.bkjia.com/PHPjc/628786.html
TechArticle
本文章来给大家介绍关于各种PHP去除多余的HTML,Javascrit,Css标签 方法与实现程序,大家可进入参考。 1.不保留任何HTML标签,代码会是这样:...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

Which 2025 currency exchanges are more secure? Which 2025 currency exchanges are more secure? Apr 20, 2025 pm 06:09 PM

The top ten safe and reliable exchanges in the 2025 cryptocurrency circle include: 1. Binance, 2. OKX, 3. Gate.io (Sesame Open), 4. Coinbase, 5. Kraken, 6. Huobi Global, 7. Gemini, 8. Crypto.com, 9. Bitfinex, 10. KuCoin. These exchanges are rated as safe and reliable based on compliance, technical strength and user feedback.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

HTML as a Markup Language: Its Function and Purpose HTML as a Markup Language: Its Function and Purpose Apr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

See all articles