PHP regular expression filtering html tag attributes (DEMO), regular expression demo_PHP tutorial

WBOY
Release: 2016-07-12 08:53:30
Original
1104 people have browsed it

PHP regular expression filtering html tag attributes (DEMO), regular expression demo

Filtering html tags has a built-in function in php, but it filters too cleanly Now, we have compiled some examples of using regular rules to filter specified html tags, as shown below.

When collecting, sometimes it is necessary to filter out redundant tag attributes. For example, the img tag filters out all attributes except the src attribute, such as deleting attributes such as titile alt and some onclick attributes.

For example

Filter all attributes except src:

Copy code The code is as follows:
$str= preg_replace('/s(?!src)[a-zA-Z] =['"]{1}[^'"] ['"]{1}/iu',' $str);

The above example code filters out all tag attributes except the src attribute.

Filter settings filter all attributes except alt and src

The code is as follows:

Copy code The code is as follows:
$str = preg_replace('/s(?!(src|alt))[a-zA-Z] =[^s]*/iu',' ', $str);

Regular expression to filter attributes of all html tags:

Copy code The code is as follows:
$str = preg_replace("/<([a-z] )[^>]*>/i","",$str );

Regular expression that only filters the alt attribute:

Copy code The code is as follows:
(s)alt=[^s]*

Regular expression to filter attributes of all html tags:

Copy code The code is as follows:
$search = array ("']*?>.*?'si", // Remove javascript
"'<[/!]*?[^<>]*?>'si", // Remove HTML tags
"'([rn])[s] '", // Remove whitespace characters
"'&(quot|#34);'i", // Replace HTML entity
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i"
); // Run as PHP code
$replace = array ("","","\1",""","&","<",">"," ");
$html = preg_replace($search, $replace, $html);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1123816.htmlTechArticlePHP regular expression filtering html tag attributes (DEMO), regular expression demo filtering html tags can be available in php There is a built-in function, but its filtering is too clean, so we compiled some advantages...
Related labels:
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