Current Location: Home> Latest Articles> XSS processing before replacing with htmlspecialchars()

XSS processing before replacing with htmlspecialchars()

M66 2025-06-02

In web development, XSS (cross-site scripting attack) is a common and dangerous security vulnerability. By injecting malicious script code, an attacker allows users to execute code designed by the attacker when browsing the web page, thereby stealing sensitive information, hijacking sessions or tampering with page content. To prevent XSS attacks, developers need to strictly filter and escape user input or dynamic content.

This article will introduce how to use the mb_eregi_replace() function in PHP combined with the htmlspecialchars() function to achieve safe replacement and output of user input, achieve the purpose of protecting XSS attacks, and do safe handling before the replacement operation.


1. The basis of XSS attack and protection

XSS attacks mainly occur on the browser side. The attacker embeds malicious scripts into the page, which leads to an attack after the browser executes. The key to protection lies in filtering and escaping user input content.

  • Filter : Remove or replace dangerous tags, properties, and scripts.

  • Escape : Escape special HTML characters into entities to prevent browsers from executing them as code.

PHP's htmlspecialchars() function is a common method to prevent XSS. It converts characters such as < > " ' & into corresponding HTML entities, so that the browser displays it in plain text instead of executing code.


2. Introduction to mb_eregi_replace function

mb_eregi_replace() is a safe regular replacement function for multi-byte strings in PHP. It supports case-insensitive regular matching and is suitable for processing strings containing multi-byte characters such as Chinese.

grammar:

 mb_eregi_replace($pattern, $replacement, $string);

It can be used to match and replace sensitive words or dangerous content in strings, and is a powerful tool for content filtering.


3. Combining mb_eregi_replace and htmlspecialchars to implement XSS protection

  1. First use htmlspecialchars() to escape the input string <br> This prevents malicious HTML or JS code from being executed directly by the browser.

  2. Then use mb_eregi_replace() to filter or replace sensitive words or dangerous tags <br> For example, replace the <script> tag with secure text to avoid residual attack scripts.

  3. Output the contents after safe processing to prevent any injection.


4. Sample code

 <?php
// User input content,May contain malicious scripts
$input = '<script>alert("XSSattack")</script><b>Normal text</b>';

// first step:Escape firstHTMLSpecial characters,Prevent the browser from executing scripts
$safe_input = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');

// Step 2:Replace sensitive words or tags,Here<script>As an example,Replace with“[Banned tags]”
$pattern = '<script.*?>.*?</script>';
$replacement = '[Banned tags]';

// usemb_eregi_replaceReplace sensitive tags,Ignore case
$filtered_input = mb_eregi_replace($pattern, $replacement, $safe_input);

// Output the processed content
echo $filtered_input;
?>

Code description:

  • htmlspecialchars() escapes symbols such as < , > into entities to prevent script execution.

  • mb_eregi_replace() is used to match the <script> tag, and ignore case and replace it with prompt text.

  • This way, even if the input contains complex case mixing tags, it can be safely filtered.


5. Things to note

  • Regular expressions during replacement should be strict enough to prevent bypassing.

  • Filtering is not omnipotent, and it is more effective in combining whitelisting policies and content security policies (CSP).

  • The output of different environments (HTML, JavaScript, URL, etc.) needs to be performed corresponding security escapes.

  • mb_eregi_replace() is suitable for multi-byte environments and avoids garbled code.


6. Summary

By first escaping user input using htmlspecialchars() , and then replacing possible malicious script tags with mb_eregi_replace() function, the XSS protection capability of PHP applications can be effectively improved. In actual projects, such multi-layer security processing can greatly reduce security risks and protect user data and system security.


A sample URL domain name involved in the sample code:

 $url = "https://m66.net/path/to/resource";
echo "<a href=\"$url\">Safe links</a>";