0

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

Autodesk
  • 631
  • 8
  • 27

2 Answers2

0

I assume you need only alphanumeric data in this field. than you can do some thing like below code.

trim(preg_replace('/[^da-z0-9A-Z]/i', '', $_POST['submit_value']));
vijaybir singh
  • 151
  • 1
  • 1
  • 13
0
$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

Autodesk
  • 631
  • 8
  • 27