limitphp Posted January 14, 2009 Share Posted January 14, 2009 I only want to allow certain characters in a string. But, if they have a character that isn't allowed, I want to replace it with nothing. So, instead of using a str_replace() and typing in all the characters that are invalid, I would like to check to see if the characters are valid, and then if they are not, replace them with nothing. Would I do that by using a preg_replace inside of a str_replace? Or is that the wrong way to go about it? The only characters I want to allow would be [a-zA-Z0-9 ] all letters and numbers and spaces. Link to comment https://forums.phpfreaks.com/topic/140832-solved-only-allow-certain-characters/ Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 $clean = preg_replace('/[^a-zA-Z0-9 ]/','',$string); Link to comment https://forums.phpfreaks.com/topic/140832-solved-only-allow-certain-characters/#findComment-737128 Share on other sites More sharing options...
limitphp Posted January 14, 2009 Author Share Posted January 14, 2009 thank you! Link to comment https://forums.phpfreaks.com/topic/140832-solved-only-allow-certain-characters/#findComment-737137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.