Regex to remove non alpha-numeric but leave spaces

I have this:

$str = ereg_replace(“[^A-Za-z0-9]”, “”, $str);

How can I allow spaces to remain in $str?

The spaces should remain for that RegEx, can you provide an example where they don’t? It will help us see what you’re expecting to happen. :slight_smile:

Actually just realised I have another function str_replacing the spaces for dashes and this happens before the function above!

Actually I was incorrect. So yes still removing spaces and I’m not sure why. I apply this to the posted field input:

$formfieldvalue = mysql_real_escape_string(htmlspecialchars(clean($_POST[$formfield])));

function clean($str){

$str = ereg_replace("[^A-Za-z0-9]", "", $str);
return $str;

}

Sorry, I was wrong. :frowning:

Here you go, note the \s for space and the migration to [FONT=courier new]preg_*

[/FONT]

<?php
function clean($value){
    return preg_replace('~[^0-9a-z\\s]~i', '', $value);
}


$string = 'Hello, I\\'m Anthony';


var_dump(
    clean($string)
);


/*
    string(16) "Hello Im Anthony"
*/


Ha! I had just found this same code via Google! Many thanks though :slight_smile: