Giuseppe Lodi Rizzini
Giuseppe Lodi Rizzini

Reputation: 1055

remove emoji in \uXXXXX format from string using php

I need to remove some emoji from string using \uXXXXX format.

$msg = "\u200b\ud83d\ude48 This is an example\nAnother line \u2199\u2199";

All function tried and found doesn't work. I don't know if it a problem of the function or my string...

example of function used:

preg_replace('/[[:^print:]]/', '', $string);

or:

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;
}

Tried replacing \x with \u inside function, but I get error.... Anyone can explain me why functions don't work?

Upvotes: 0

Views: 892

Answers (1)

oriberu
oriberu

Reputation: 1216

Are you sure your text includes actual symbols and not just \uxxxx strings like your example? This:

$text = "😃 This is an example. 😂";
print( preg_replace('/[\x{1F600}-\x{1F64F}]+/u', '', $text) );

should actually produce:

' This is an example. '

Edit: Otherwise, just match those string representations of unicode characters in your text with something like /\\u[0-9a-fA-F]{4}/g and replace them. For example:

$msg = "\u200b\ud83d\ude48 This is an example\nAnother line \u2199\u2199";
print( preg_replace('/\\\u[0-9a-fA-F]{4}/', '', $msg) );

Output:

" This is an example
Another line "

Upvotes: 1

Related Questions