Hello. I'd like to transfer a dynamic string called $string so that all the letters are converted to lowercase, all spaces are converted to underscore and everything different from numbers, letters and underscore should be removed. Also, white space from inside and around the string should be removed too if possible. I can start with: // remove bad characters here, everything else than numbers, letters and underscore from the string $string=strtolower($string); // white space should be removed here, including double and triple spaces between words, white space around the whole string, etc $string=str_replace(" ","_",$string); PHP:
As you asked for; $string="HEllo world1234 ffas 9999()() 4fh"; $string = preg_replace("/\s+/", " ", $string); $string = str_replace(" ", "_", $string); $string = preg_replace("/[^A-Za-z0-9_]/","",$string); $string=strtolower($string); print $string; PHP:
It converts multiple spaces, into a single space instead; obviously not required if you dont mind having multiple underscores in their place