Advertisement
Guest User

filter allowed chars using PHP

a guest
Mar 30th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. function allowed_chars($text){
  2.     $allowed = array(
  3.         "1","2","3","4","5","6","7","8","9","0",
  4.         "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
  5.         "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
  6.         "ا","أ","إ","آ","ض","ص","ث","ق","ف","غ","ع","ه","خ","ح","ج","د","ش","س","ي","ب","ل","ت","ن","م","ك","ط","ئ","ء","ؤ","ر","ى","ة","و","ز","ظ","ذ"
  7.         ," ","-","_"
  8.     );
  9.     $splitted = str_split_utf8($text);
  10.     $text = "";
  11.     foreach($splitted as $letter){
  12.         if(in_array($letter,$allowed)){
  13.             $text .= $letter;
  14.         }
  15.     }
  16.     return $text;
  17. }      
  18.  
  19.  
  20. function str_split_utf8($str) {
  21.     // place each character of the string into array
  22.     $split=1;
  23.     $array = array();
  24.     for ( $i=0; $i < strlen( $str ); ){
  25.         $value = ord($str[$i]);
  26.         if($value > 127){
  27.             if($value >= 192 && $value <= 223)
  28.                 $split=2;
  29.             elseif($value >= 224 && $value <= 239)
  30.                 $split=3;
  31.             elseif($value >= 240 && $value <= 247)
  32.                 $split=4;
  33.         }else{
  34.             $split=1;
  35.         }
  36.         $key = NULL;
  37.         for ( $j = 0; $j < $split; $j++, $i++ ) {
  38.             $key .= $str[$i];
  39.         }
  40.         array_push( $array, $key );
  41.     }
  42.     return $array;
  43. }
  44.  
  45.  
  46. //Enjoy it :) Khashabawy.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement