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);