note 13682 added to function.ereg-replace.php

From: Date: Thu, 28 Jun 2001 19:27:32 +0000
Subject: note 13682 added to function.ereg-replace.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-14732@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 http://www.php.net/manual/en/function.ereg-replace.php

« previous php.notes (#14732) next »