When processing user input or dynamically generated content, PHP developers often need to replace strings, while ensuring that these data remains structurally complete and secure when transmitted using json_encode() on the front end. mb_eregi_replace is a function in PHP multi-byte string extension mbstring that is more reliable than traditional preg_replace when dealing with multilingual or Unicode strings. This article will introduce how to use mb_eregi_replace to replace data and combine json_encode() to achieve secure data transmission.
mb_eregi_replace() is a multibyte-safe regular replacement function with the following syntax:
mb_eregi_replace(string $pattern, string $replacement, string $string, string $option = ""): string|false
Similar to preg_replace , it also supports regular expressions, but unlike mb_ereg_replace , it is a case-insensitive version (equivalent to the default i modifier on).
Imagine a typical scenario: you need to replace the URL in the string, replace all links starting with http or https with safe placeholders, and avoid directly displaying content uploaded by users as clickable links.
$input = "Welcome to our website:https://m66.net/page?id=123,Learn more。";
$pattern = "(https?://[^\s]+)";
$replacement = "[Link blocked]";
$cleaned = mb_eregi_replace($pattern, $replacement, $input);
echo $cleaned;
Output result:
Welcome to our website:[Link blocked],Learn more。
When we transfer the replaced content to the front end (such as through AJAX), we need to encode it using json_encode() . If the replacement result contains special characters such as " , \n , etc., direct transmission may destroy the JSON format.
Let’s take a look at an example:
$data = [
"message" => mb_eregi_replace($pattern, $replacement, $input)
];
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo $json;
Output result:
{"message":"Welcome to our website:[Link blocked],Learn more。"}
JSON_UNESCAPED_UNICODE and JSON_UNESCAPED_SLASHES are used here, which avoids redundant escaping of Chinese characters and URLs, making the front-end easier to handle.
Sometimes we don’t just want to replace the entire URL, but we want to retain some information, such as domain names. We can use anonymous functions to implement it with callbacks:
$pattern = "(https?://([a-z0-9\-\.]+)(/[^\s]*))";
$replacement = function ($matches) {
return "Links from domain name:" . $matches[2]; // Extract the domain name part
};
$input = "Click here:https://m66.net/product/view?id=10 check the details";
$cleaned = preg_replace_callback("/$pattern/i", $replacement, $input);
echo json_encode(["message" => $cleaned], JSON_UNESCAPED_UNICODE);
Output:
{"message":"Click here:Links from domain name:m66.net check the details"}
This method can flexibly process URL information, and even be used for security policies such as statistical sources and domain name filtering.
Through mb_eregi_replace , we can efficiently and securely replace string content in multiple languages; combined with the reasonable use of json_encode() , we can ensure that the structure and content of the data during transmission are not corrupted. This combination is very practical in modern PHP development, especially in the construction of international applications, rich text processing, security review and other scenarios.
Mastering the combined application of these two functions will greatly improve your ability in data preprocessing and secure transmission.