I'm using Smarty to pass in and display the contents of a first_name
variable. Some users have Emoji characters (http://en.wikipedia.org/wiki/Emoji) in their first_name
and I am wondering how I can either a) conditionally not display a user's first_name
if it contains emojis or b) filter out emoji characters from first_name
. Can this be done with Smarty? Can it be done with PHP in Smarty?
Removing emojis from variable
IT is just (correct me if im wrong) U+F000 to U+FFFF –
Typescript
The emoji are encoded in the block U+1F300–U+1F5FF.
preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $first_name)
this will strip those out
I tried this with no luck, but I'm not sure if I am implementing correctly. Here's how I tried coding it: jsbin.com/epefej/1/edit. –
Glimmer
@585connor: You need to do something like
$first_name = preg_replace('~\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]~', '', $first_name); echo $first_name;
from PHP of course (PHP open / close tags are <?php
and ?>
). –
Spruik It is not matching in preg_replace. I just have emoji symbols(😀 😁 😂) how to decode it like x80....? So that i can preg_replace? –
Benedicto
According to wiki, emojis are encoded from U+1F600 to U+1F64F: en.wikipedia.org/wiki/Emoticons_(Unicode_block). Where did you get your numbers from? –
Gav
❤️ : this is double hearts at same location !! I have remove it : $text = str_replace("❤️","",$text); –
Reverberatory
I tried some of the solutions posted above, but no one worked, however, when I converted the string to UTF-8 using the mb_ function it works properly.
You can use:
trim( preg_replace('/[\x00-\x1F\x80-\xFF]/', '',
mb_convert_encoding( $emojiString, "UTF-8" ) ) );
Works for me.
Upvoted! This is the 6th solution I found on SO, and the only one that worked for the particular string with which I was dealing. –
Eolian
This will also remove special characters like ñ or é –
Laquanda
function remove_emoji($string) {
// Match Emoticons
$regex_emoticons = '/[\x{1F600}-\x{1F64F}]/u';
$clear_string = preg_replace($regex_emoticons, '', $string);
// Match Miscellaneous Symbols and Pictographs
$regex_symbols = '/[\x{1F300}-\x{1F5FF}]/u';
$clear_string = preg_replace($regex_symbols, '', $clear_string);
// Match Transport And Map Symbols
$regex_transport = '/[\x{1F680}-\x{1F6FF}]/u';
$clear_string = preg_replace($regex_transport, '', $clear_string);
// Match Miscellaneous Symbols
$regex_misc = '/[\x{2600}-\x{26FF}]/u';
$clear_string = preg_replace($regex_misc, '', $clear_string);
// Match Dingbats
$regex_dingbats = '/[\x{2700}-\x{27BF}]/u';
$clear_string = preg_replace($regex_dingbats, '', $clear_string);
return $clear_string;
}
Found this answer at https://medium.com/coding-cheatsheet/remove-emoji-characters-in-php-236034946f51
This one is only one from this thread removing thumbs up emoji. (By the way this symbol was breaking saving string to utf8_general_ci field.) –
Centro
Q: Can this be done with Smarty? A: Yes.
Q: Can it be done with PHP in Smarty? A: Yes. But please don't use PHP tags on template side.
Try to use a variable modifier on a template variable instead.
{* apply modifier to a variable *}
{$first_name|emojistrip}
Put the following content into a file named "modifier.emojistrip.php" in the folder "/smarty/plugins/".
function smarty_modifier_emojistrip($string)
{
return preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $string);
}
© 2022 - 2024 — McMap. All rights reserved.