PHP implements filtering various HTML tags_PHP tutorial
Jul 13, 2016 am 09:53 AMPHP implements filtering of various HTML tags
In the process of doing projects, we often need to filter some HTML tags to improve data security. In fact, it is to delete those Data that is potentially harmful to the application. It is used to strip tags and remove or encode unwanted characters.
First share some common ones
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
$str=preg_replace("/
$str=preg_replace("/s /","", $str); //Filter redundant carriage returns
$str=preg_replace("/<[ ] /si","<",$str); //Filter <__("<" with a space after it)
$str=preg_replace("//si","",$str); //Comments
$str=preg_replace("/<(!.*?)>/si","",$str); //Filter DOCTYPE
$str=preg_replace("/<(/?html.*?)>/si","",$str); //Filter html tags
$str=preg_replace("/<(/?head.*?)>/si","",$str); //Filter head tag
$str=preg_replace("/<(/?meta.*?)>/si","",$str); //Filter meta tags
$str=preg_replace("/<(/?body.*?)>/si","",$str); //Filter body tag
$str=preg_replace("/<(/?link.*?)>/si","",$str); //Filter link tags
$str=preg_replace("/<(/?form.*?)>/si","",$str); //Filter form tags
$str=preg_replace("/cookie/si","COOKIE",$str); //Filter COOKIE tags
$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //Filter applet tag
$str=preg_replace("/<(/?applet.*?)>/si","",$str); //Filter applet tags
$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //Filter style tag
$str=preg_replace("/<(/?style.*?)>/si","",$str); //Filter style tag
$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //Filter title tag
$str=preg_replace("/<(/?title.*?)>/si","",$str); //Filter title tag
$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //Filter object tag
$str=preg_replace("/<(/?objec.*?)>/si","",$str); //Filter object tag
$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //Filter noframes tag
$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //Filter noframes tag
$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str) ; //Filter frame tag
$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //Filter frame tag
$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //Filter script tag
$str=preg_replace("/<(/?script.*?)>/si","",$str); //Filter script tags
$str=preg_replace("/javascript/si","Javascript",$str); //Filter script tags
$str=preg_replace("/vbscript/si","Vbscript",$str); //Filter script tags
$str=preg_replace("/on([a-z] )s*=/si","On1=",$str); //Filter script tags
$str=preg_replace("http://si","",$str); //Filter script tags |
A simpler way of writing:
?
2 3
|
function delhtml($str){ //Clear html tag
$st=-1; //Start
$et=-1; //End
$stmp=array();
$stmp[]=" ";
$len=strlen($str);
for($i=0;$i<$len;$i ){?>
?>$ss=substr($str,$i,1);?>
?>if(ord($ss)==60){ //ord("<")==60?>
?>$st=$i;?>
?>}?>
?>if(ord($ss)==62){ //ord(">")==62
$et=$i;
if($st!=-1){
$stmp[]=substr($str,$st,$et-$st 1);
}
}
}
$str=str_replace($stmp,"",$str);
return $str;
}
One more one:
?
|

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

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

exit() is a function in PHP that is used to terminate script execution immediately. Common uses include: 1. Terminate the script in advance when an exception is detected, such as the file does not exist or verification fails; 2. Output intermediate results during debugging and stop execution; 3. Call exit() after redirecting in conjunction with header() to prevent subsequent code execution; In addition, exit() can accept string parameters as output content or integers as status code, and its alias is die().

To merge two PHP arrays and keep unique values, there are two main methods. 1. For index arrays or only deduplication, use array_merge and array_unique combinations: first merge array_merge($array1,$array2) and then use array_unique() to deduplicate them to finally get a new array containing all unique values; 2. For associative arrays and want to retain key-value pairs in the first array, use the operator: $result=$array1 $array2, which will ensure that the keys in the first array will not be overwritten by the second array. These two methods are applicable to different scenarios, depending on whether the key name is retained or only the focus is on

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

loading="lazy" is an HTML attribute for and which enables the browser's native lazy loading function to improve page performance. 1. It delays loading non-first-screen resources, reduces initial loading time, saves bandwidth and server requests; 2. It is suitable for large amounts of pictures or embedded content in long pages; 3. It is not suitable for first-screen images, small icons, or lazy loading using JavaScript; 4. It is necessary to cooperate with optimization measures such as setting sizes and compressing files to avoid layout offsets and ensure compatibility. When using it, you should test the scrolling experience and weigh the user experience.

To access session data in PHP, you must first start the session and then operate through the $_SESSION hyperglobal array. 1. The session must be started using session_start(), and the function must be called before any output; 2. When accessing session data, check whether the key exists. You can use isset($_SESSION['key']) or array_key_exists('key',$_SESSION); 3. Set or update session variables only need to assign values ??to the $_SESSION array without manually saving; 4. Clear specific data with unset($_SESSION['key']), clear all data and set $_SESSION to an empty array.

The way to process raw POST data in PHP is to use $rawData=file_get_contents('php://input'), which is suitable for receiving JSON, XML, or other custom format data. 1.php://input is a read-only stream, which is only valid in POST requests; 2. Common problems include server configuration or middleware reading input streams, which makes it impossible to obtain data; 3. Application scenarios include receiving front-end fetch requests, third-party service callbacks, and building RESTfulAPIs; 4. The difference from $_POST is that $_POST automatically parses standard form data, while the original data is suitable for non-standard formats and allows manual parsing; 5. Ordinary HTM

There are two ways to create an array in PHP: use the array() function or use brackets []. 1. Using the array() function is a traditional way, with good compatibility. Define index arrays such as $fruits=array("apple","banana","orange"), and associative arrays such as $user=array("name"=>"John","age"=>25); 2. Using [] is a simpler way to support since PHP5.4, such as $color

When the Windows search bar cannot enter text, common solutions are: 1. Restart the Explorer or computer, open the Task Manager to restart the "Windows Explorer" process, or restart the device directly; 2. Switch or uninstall the input method, try to use the English input method or Microsoft's own input method to eliminate third-party input method conflicts; 3. Run the system file check tool, execute the sfc/scannow command in the command prompt to repair the system files; 4. Reset or rebuild the search index, and rebuild it through the "Index Options" in the "Control Panel". Usually, we start with simple steps first, and most problems can be solved step by step.
