duankangpazhuo0347 2017-08-01 07:49
浏览 195
已采纳

如何在PHP中修剪字符DEL(ASCII代码127)

I trim post value after form submitted, like this:

$value=trim($_POST['submit_value']);

So if I submit ' some_value ', then it will become 'some_value'

PHP trim() works fine so far.

But recently, there is an DEL(ASCII code 127) character submitted and I don't know how to trim it.

$asd=DEL(ASCII code 127) character;
    // I cannot copy, because stackoverflow remove it.
    // it looks like space but it's not. trim() doesn't work. 

echo ord($asd); //return 127

any ideas? many thanks!


btw, here is a way to type this character:

Unicode Character 'DELETE' (U+007F)

How to type in Microsoft Windows

I don't know under what condition will a user need to type this character when register his name

  • 写回答

2条回答 默认 最新

  • dongzilu0178 2017-09-10 14:59
    关注
    $string = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $string);
    

    It matches anything in range 0-31, 127-255 and removes it.

    The answer is from: PHP: How to remove all non printable characters in a string?


    BTW, one may replace multiple spaces with a single space:

    $output = preg_replace('!\s+!', ' ', $input);
    

    php Replacing multiple spaces with a single space

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?