如何在php中过滤ascii控制字符

这篇文章将为大家详细讲解有关如何在php中过滤ascii控制字符,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

代码如下:


/**
 * 根据ascii码过滤控制字符
 * @param type $string
 */
public static function special_filter($string)
{
 if(!$string) return '';

 $new_string = '';
 for($i =0; isset($string[$i]); $i++)
 {
  $asc_code = ord($string[$i]);    //得到其asc码

  //以下代码旨在过滤非法字符
  if($asc_code == 9 || $asc_code == 10 || $asc_code == 13){
   $new_string .= ' ';
  }
  else if($asc_code > 31 && $asc_code != 127){
   $new_string .= $string[$i];
  }
 }

 return trim($new_string);
}

关于如何在php中过滤ascii控制字符就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。