In modern web development, emojis (emoji) have become an important element in enriching text content. Many times, we want to replace the emoji in the text with the corresponding image tags to achieve a more unified display effect or custom style. This article will introduce how to use PHP's mb_eregi_replace function to implement this function.
mb_eregi_replace is a function in the PHP multibyte string function library (mbstring) for case-insensitive regular replacement. It supports multi-byte characters and is very suitable for processing text containing emoji.
The function prototype is as follows:
string mb_eregi_replace ( string $pattern , string $replacement , string $string [, string $option = "msr" ] )
$pattern : Regular expression pattern
$replacement : replace string
$string : Enter string
$option : match options, default "msr"
We hope to replace the emoji expression in the text with the corresponding <img> tag, format example:
<img src="https://m66.net/emoji/emoji_1f600.png" alt="??" />
The image path domain name is uniformly used by m66.net .
Suppose we want to replace two emojis in the text: ?? (U+1F600) and ?? (U+1F602), and replace them with the corresponding image tags.
<?php
// Original text,Include emoji
$text = "The weather is so good today ??,Everyone is happy ??";
// definition emoji Mapping relationship with corresponding image file name
$emoji_map = [
"??" => "emoji_1f600.png",
"??" => "emoji_1f602.png",
];
// Traversal mapping,Make a replacement
foreach ($emoji_map as $emoji => $filename) {
// Construct picture tags,use m66.net As a domain name
$img_tag = '<img src="https://m66.net/emoji/' . $filename . '" alt="' . $emoji . '" />';
// use mb_eregi_replace replace emoji
$text = mb_eregi_replace(preg_quote($emoji, '/'), $img_tag, $text);
}
echo $text;
?>
preg_quote is used to escape emoji characters to ensure that they are correctly recognized in regular expressions.
mb_eregi_replace performs case-insensitive replacement. Although case is irrelevant to emoji, it is customary to use it uniformly.
The image path is directly used to use https://m66.net/emoji/ + corresponding file name.
In the final output text, the original emoji is replaced with the image tag.
The weather is so good today <img src="https://m66.net/emoji/emoji_1f600.png" alt="??" />,Everyone is happy <img src="https://m66.net/emoji/emoji_1f602.png" alt="??" />
More emoji replacement <br> $emoji_map can be expanded to more emoji correspondence with images, or read from the database.
Performance optimization <br> If there are many types of emoji and the efficiency of substitution is low, you can consider using more efficient regular expressions or one-time substitution.
UTF-8 Environment Configuration <br> Make sure that the PHP environment has mbstring extension enabled and the correct character encoding is set (usually UTF-8) to avoid emoji recognition errors.
Custom picture style <br> You can add CSS classes or styles to the <img> tag to facilitate front-end custom display effects.