note 13682 deleted from function.ereg-replace by jimw

From: Date: Fri, 02 Nov 2001 01:58:41 +0000
Subject: note 13682 deleted from function.ereg-replace by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-20214@lists.php.net to get a copy of this message
#### Example of function using ereg_replace to take string and get rid of special characters. #### --> extract alphanumerics, plus parm-passed characters (this part could probably be done better, but works) #### eliminates duplicate spaces, and optionally convert spaces to another character #### i use it to clean user-entered form data, especially for MySQL. function washstring($str, $changespacesto, $allow1, $allow2) { $str = ereg_replace("[^[:alnum:][:space:]$allow1$allow2]","",$str); #for test: echo "str=$str"; #eliminate duplicate spaces $str = ereg_replace("[[:space:]]+", " ", $str); #for test: echo "str=$str"; #convert spaces to whatever is passed in, could be dash, space, whatever. $str = str_replace (" ","$changespacesto", $str); #for test: echo "str=$str"; return $str; } $test = 'Blah-blah--~@#$%^&*()_o+z=z+y r@=u{l}e|\s]![:";<>?/.,' . "'---BlechBlech"; # test it: nix all special chars except, for example: ! and -. $temptest= washstring($test, '_', '!','-') ; echo "temptest=$temptest"; cheers, ben nardone

« previous php.notes (#20214) next »