In PHP programming, we often use regular expression functions when dealing with string substitution. Many developers may have heard of the function mb_eregi_replace , but do you know it is actually case-insensitive? This has an important impact on case problems in regular mode.
mb_ereg_replace is a regular replacement function used in PHP for multi-byte strings. Its characteristic is case-insensitive , which is in sharp contrast to mb_ereg_replace (case-sensitive). The "i" here represents "ignore case", which means ignoring upper and lower case.
The definition form of the function is as follows:
mb_eregi_replace($pattern, $replacement, $string, $option = "msr")
$pattern is a regular expression pattern and does not require additional case modifier i .
$replacement is the replacement content.
$string is the target string.
$option is a matching pattern and can be customized.
In the traditional preg_replace function, if you want to ignore case, you need to add an i modifier at the end of the regular expression, for example:
preg_replace('/php/i', 'PHP', 'I love Php');
The result will replace all case "php".
However, mb_eregi_replace is born to ignore upper and lower case without adding i , for example:
<?php
$original = "Hello Mb_EreGi_ReplAce Function!";
$result = mb_eregi_replace("mb_eregi_replace", "mb_eregi_replace", $original);
echo $result;
?>
Regardless of whether the input is MB_EREGI_REPLACE , mb_eregi_replace , or Mb_EreGi_RePlAcE , it can be replaced with the correct match.
<?php
$text = "Visit https://m66.net for more info.";
$pattern = "M66.NET";
$replacement = "example.com";
// use mb_eregi_replace Case insensitive replacement
$result = mb_eregi_replace($pattern, $replacement, $text);
echo $result;
?>
Output:
Visit https://example.com for more info.
Note that $pattern is uppercase, and the URL in $text is lowercase, but it still matches successfully.
When dealing with URLs, mailboxes, file paths, etc., the case sensitivity is huge. Especially when using a multi-byte character, using a normal regular function may cause garbled code or matching failure.
mb_eregi_replace uses a multi-byte-safe matching mechanism internally, while ignoring upper and lower case, simplifying encoding work.
mb_eregi_replace is a case-insensitive multi-byte regular replacement function.
There is no need to manually add an i modifier.
Suitable for handling multibyte strings and case-insensitive replacement requirements.
When replacing URL domain names, there is no need to worry about the case of the domain name.
Next time you use regular replacement in PHP, remember to select functions reasonably to avoid matching failures caused by case problems. mb_eregi_replace can save you a lot of trouble.