How to identify control characters in a url and remove those using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pramod K S

    How to identify control characters in a url and remove those using php

    Hello,

    How to identify control characters in a url and remove those using php?

    Thanks.
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    Control characters? Like, ASCII control characters? You could either just use urlencode(), or you could go through the string removing characters with the ASCII value 0 to 31 and 127.

    Code:
    function kill_ctrl_chars($data) {
    	$formatted_data = '';
    	
    	for ($i = 0, $max = strlen($data); $i < $max; $i++) {
    		$ascii_value = ord($data[$i]);
    		
    		if (($ascii_value > 31 || $ascii_value < 0) && $ascii_value != 127) {
    			$formatted_data .= $data[$i];
    		}
    	}
    	
    	return $formatted_data;
    }

    Comment

    Working...