過濾ASCII控制碼
發表於 : 2018-08-03 09:17:29
https://stackoverflow.com/questions/149 ... php-string
85
down vote
accepted
If you mean by control characters the first 32 ascii characters and \x7F (that includes the carriage return, etc!), then this will work:
(Note the single quotes: with double quotes the use of \x00 causes a parse error, somehow.)
The line feed and carriage return (often written \r and \n) may be saved from removal like so:
I must say that I think Bobby's answer is better, in the sense that [:cntrl:] better conveys what the code does than [\x00-\x1F\x7F].
WARNING: ereg_replace is deprecated in PHP >= 5.3.0 and removed in PHP >= 7.0.0!, please use preg_replace instead of ereg_replace:
85
down vote
accepted
If you mean by control characters the first 32 ascii characters and \x7F (that includes the carriage return, etc!), then this will work:
代碼: 選擇全部
preg_replace('/[\x00-\x1F\x7F]/', '', $input);
The line feed and carriage return (often written \r and \n) may be saved from removal like so:
代碼: 選擇全部
preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $input);
WARNING: ereg_replace is deprecated in PHP >= 5.3.0 and removed in PHP >= 7.0.0!, please use preg_replace instead of ereg_replace:
代碼: 選擇全部
preg_replace('/[[:cntrl:]]/', '', $input);