十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹PHP如何使用正則過濾處理微信昵稱中emoji字符,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體如下:
今天剛做了一個微信應(yīng)用,在獲取微信昵稱的過程中報錯了,經(jīng)查原因是微信昵稱中包含emoji字符,在寫入數(shù)據(jù)庫的時候出錯,所以想辦法在寫入之前把這些字符過濾掉,于是在網(wǎng)上找到一個方法,記錄一下。
移除微信昵稱中的emoji字符:
function removeEmoji($nickname) { $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; }
另外還發(fā)現(xiàn)一個github開源應(yīng)用,還沒有研究測試。
https://github.com/iamcal/php-emoji
補充:今天又在網(wǎng)上找到一個更簡單的方法
// 過濾掉emoji表情 function filterEmoji($str) { $str = preg_replace_callback( '/./u', function (array $match) { return strlen($match[0]) >= 4 ? '' : $match[0]; }, $str); return $str; }
以上是“PHP如何使用正則過濾處理微信昵稱中emoji字符”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!