Remove non-ascii characters from string

前端 未结 8 1279
遥遥无期
遥遥无期 2020-11-28 03:39

I\'m getting strange characters when pulling data from a website:

Â

How can I remove anything that isn\'t a non-extended ASCII character?

8条回答
  •  囚心锁ツ
    2020-11-28 04:13

    I think the best way to do something like this is by using ord() command. This way you will be able to keep characters written in any language. Just remember to first test your text's ord results. This will not work on unicode.

    $name="βγδεζηΘKgfgebhjrf!@#$%^&";    
    //this function will clear all non greek and english characters on greek-iso charset        
    function replace_characters($string)    
    {    
       $str_length=strlen($string);    
       for ($x=0;$x<$str_length;$x++)    
          {    
              $character=$string[$x];    
              if ((ord($character)>64 && ord($character)<91) || (ord($character)>96 && ord($character)<123) || (ord($character)>192 && ord($character)<210) || (ord($character)>210 && ord($character)<218) || (ord($character)>219 && ord($character)<250) || ord($character)==252 || ord($character)==254)    
                 {    
                     $new_string=$new_string.$character;     
                 }    
          }    
          return $new_string;    
    }    
    //end function    
    
    $name=replace_characters($name);    
    
    echo $name;    
    

提交回复
热议问题