【问题标题】:PHP: Remove unprintable characters without removing white spacePHP:删除不可打印字符而不删除空格
【发布时间】:2009-08-26 19:35:01
【问题描述】:

我知道这是一个常见问题,但我发现的所有内容似乎都删除了空格。

我正在寻找一个正则表达式,它可以在不更改任何空格的情况下去除不可打印的字符。这是一个过滤所有用户输入的功能,这意味着您通常可以在键盘上键入的所有字符都是有效的。例如:您在西班牙语中看到的口音是有效的。基本上可以使用 UTF 8 字符集显示的任何内容。

因为这是 SQL Server,我认为“SET NAMES UTF8”方法行不通。

这就是我所拥有的。

function stripNonPrintable($input) 
{
    return preg_replace('/[\x00\x08\x0B\x0C\x0E-\x1F]/', '', $input);
}

【问题讨论】:

    标签: php sql-server


    【解决方案1】:

    试试这样的:

    function stripNonPrintable($input) {
       $bad=array(
          '\x00\x08\x0B\x0C\x0E-\x1F'
       );
       $fixed=array(
          ''
       );
       return str_replace($bad, $fixed, $input);
    }
    

    【讨论】:

    • 简单但有效。谢谢。
    【解决方案2】:

    你总是可以先转义空格:

        function stripNonPrintable($input) 
        {
            $input = preg_replace('/ /','%%%%SPACE%%%%%%', $input);
            $input = preg_replace('/\t/','%%%%TAB%%%%%%', $input);
            $input = preg_replace('/\n/','%%%%NEWLINE%%%%%%', $input);
    
            $input = preg_replace('/[\x00\x08\x0B\x0C\x0E-\x1F]/', '', $input);
    
            $input = str_replace('%%%%SPACE%%%%%%',    ' ', $input);
            $input = str_replace('%%%%TAB%%%%%%',     "\t", $input);
            $input = str_replace('%%%%NEWLINE%%%%%%', "\n", $input);
    
        }
    

    不优雅,但它有效。

    【讨论】:

      猜你喜欢
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2012-01-02
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多