PHP:寫一個簡單的removeEmoji 函數
問題:
問題:如何建立一個🎜>
<code class="php">public static function removeEmoji($string) { // split the string into UTF8 char array // for loop inside char array // if char is emoji, remove it // endfor // return newstring }</code>
建議實作:
建議實作:<code class="php">public static function removeEmoji($text) { $clean_text = ""; // Match Emoticons $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u'; $clean_text = preg_replace($regexEmoticons, '', $text); // Match Miscellaneous Symbols and Pictographs $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u'; $clean_text = preg_replace($regexSymbols, '', $clean_text); // Match Transport And Map Symbols $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u'; $clean_text = preg_replace($regexTransport, '', $clean_text); // Match Miscellaneous Symbols $regexMisc = '/[\x{2600}-\x{26FF}]/u'; $clean_text = preg_replace($regexMisc, '', $clean_text); // Match Dingbats $regexDingbats = '/[\x{2700}-\x{27BF}]/u'; $clean_text = preg_replace($regexDingbats, '', $clean_text); return $clean_text; }</code>
雖然建議的實作利用循環來辨識與刪除表情符號,但使用preg_replace 函數有更有效的解決方案。
此函數針對特定的 Unicode 範圍來識別並從輸入文字中刪除表情符號。請參閱 unicode.org - 完整表情符號清單 以了解其他表情符號字元範圍。以上是如何用 PHP 有效率地從 Instagram 評論中刪除表情符號?的詳細內容。更多資訊請關注PHP中文網其他相關文章!