如何在 php 中检查文本框中的表情符号数量


How to check number of emoticons in textbox in php?

我正在解除来自 android 的文本作为帖子数据,这可能是字母或表情符号,如何检查如果表情符号在 php 的服务器端不应大于 25。

请帮助我谢谢

你需要substr_count SPL的函数,参考:function.substr-count.php

//Maximum numbers of emoticons
$iMaxEmoticons = 3;
//List of emoticons, each of them
$aEmoticonsList = array(
    ':)',
    ';)',
    ':>',
    ':(',
    //......
);
$sMessage = $_POST['androidMessage'];
$iEmoticonsCount = 0;
//Count each occurrence of emoticons
foreach ($aEmoticonsList as $sEmoticon) {
    //for utf8, use mb_substr_count instead
    $iEmoticonsCount += substr_count($sMessage, $sEmoticon);
}
//Check if maximum of emoticons is reached, print message and exit
if($iEmoticonsCount > $iMaxEmoticons){
    exit("Error max of ({$iMaxEmoticons}) emoticons reached, count {$iEmoticonsCount}.");
}

使用 mb_substr_count(用于计算子字符串出现的次数)。

//Function to return array of check emoticons...
function emoticons() {
         $arrIcons = array(
                                        ':)',
                        ':-)',
                        ':D',
                        ':d',
                        ';)',
                        ':P',
                        ':-P',
                        ':-p',
                        ':p',
                        ':(',
                        ':o',
                        ':O',
                        ':0',
                        ':|',
                        ':-|',
                        ':/',
                        ':-/'
        );
        return $arrIcons;
}

//Check for emoticons...
$maxAllowIcon = 25;
$txtMYTEXTBOX = $_REQUEST['MYTEXTBOX'];//Get textbox inputed value...
$arrExistIcon = emoticons();//Get predefined icons...
//Check for how many emoticons used...
$cntEmoticons = 0;
foreach($arrExistIcon AS $keyImoticons => $emotIcons){
    $cntEmoticons += mb_substr_count($txtMYTEXTBOX, $emotIcons);    
}
//If icons more then maximum allowed then print message...
if($cntEmoticons > $maxAllowIcon){
    print('ERROR :: Maximum ' . $maxAllowIcon . ' emoticons allowed');
    exit;
}

你想在安卓或服务器上检查它吗?