2016-03-21 20 views

Antwort

0

Sie wollen es auf Android oder Server überprüfen?

+0

Serverseite in PHP, habe ich die Frage bearbeitet .. –

+0

@UmashankarSaw was meinen Sie mit Emoticon? ist es Text so ist Ihr Datenformat eine Zeichenkette, die Alphabete und einige Zahlen enthält? – techblu3

2

Sie benötigen substr_count Funktion von SPL, Referenz: 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}."); 
} 
+0

Ich empfange Daten im utf8-Format wie "\ uD83D \ uUDE1D" für ein Emoticon kann auch anderes sein –

+0

Test erneut mit mb_substr_count –

1

Verwenden mb_substr_count (für die Anzahl der Teilzeichen Vorkommen zählen).

//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; 
}