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

时间:2016-03-21 07:16:58

标签: php maxlength emoticons

我正在释放来自android的文本作为发布数据,这可能是字母或表情符号,如何检查表情符号在php中的服务器端是否应该不大于25。

请帮帮我 感谢

3 个答案:

答案 0 :(得分:2)

您需要SPL的substr_count函数,参考: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}.");
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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