Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Created September 20, 2020 01:53
Show Gist options
  • Save fhferreira/cccc44f3c1b07ca93d1645b23f383490 to your computer and use it in GitHub Desktop.
Save fhferreira/cccc44f3c1b07ca93d1645b23f383490 to your computer and use it in GitHub Desktop.
<?php
use Closure;
class RemoveEmoji
{
/**
* Clean not allowed UTF-8 chars
*
* @param $text
* @return mixed
*/
protected function removeEmoji($text)
{
$text = mb_convert_encoding($text, "UTF-8");
// Match Emoticons
$regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
$cleanText = preg_replace($regexEmoticons, '', $text);
// Match Miscellaneous Symbols and Pictographs
$regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
$cleanText = preg_replace($regexSymbols, '', $cleanText);
// Match Transport And Map Symbols
$regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
$cleanText = preg_replace($regexTransport, '', $cleanText);
// Match Miscellaneous Symbols
$regexMisc = '/[\x{2600}-\x{26FF}]/u';
$cleanText = preg_replace($regexMisc, '', $cleanText);
// Match Dingbats
$regexDingbats = '/[\x{2700}-\x{27BF}]/u';
$cleanText = preg_replace($regexDingbats, '', $cleanText);
// Match Flags
$regexDingbats = '/[\x{1F1E6}-\x{1F1FF}]/u';
$cleanText = preg_replace($regexDingbats, '', $cleanText);
// Others
$regexDingbats = '/[\x{1F910}-\x{1F95E}]/u';
$cleanText = preg_replace($regexDingbats, '', $cleanText);
$regexDingbats = '/[\x{1F980}-\x{1F991}]/u';
$cleanText = preg_replace($regexDingbats, '', $cleanText);
$regexDingbats = '/[\x{1F9C0}]/u';
$cleanText = preg_replace($regexDingbats, '', $cleanText);
$regexDingbats = '/[\x{1F9F9}]/u';
$cleanText = preg_replace($regexDingbats, '', $cleanText);
return $cleanText;
}
/**
* Remove emoji from request
*
* @param $item
* @param $key
*/
public function cleanUp(&$item, $key)
{
if (is_string($item) && strlen($item) > 0) {
$item = $this->removeEmoji($item);
}
}
/**
* Handle an incoming request. (use as a middleware)
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!$request->isMethod('GET')) {
$input = $request->all();
array_walk_recursive($input, [$this, 'cleanUp']);
$request->replace($input);
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment