rgrne Posted April 10, 2007 Share Posted April 10, 2007 I need to strip out non alphanumeric characters from usernames. Just stripping them out would be OK, but replacing them with some other character would be better. Is there any easy way to do this, like an equivalent of strip_tags for html characters? I'm looking into eregi and eregi_replace, but it seems like I'd have to list all possible non-alphanumeric characters and go through letter by letter. Am I missing something? Link to comment https://forums.phpfreaks.com/topic/46365-easy-way-to-strip-out-non-alphanumeric-characters/ Share on other sites More sharing options...
fert Posted April 10, 2007 Share Posted April 10, 2007 $str=preg_replace("/^[a-zA-z]/","a",$str); this code will replace all non letters with the letter a, but I'm not sure if it'll work because the ^ character has two uses in regex. Link to comment https://forums.phpfreaks.com/topic/46365-easy-way-to-strip-out-non-alphanumeric-characters/#findComment-225525 Share on other sites More sharing options...
Glyde Posted April 10, 2007 Share Posted April 10, 2007 Should be: $cleanString = preg_replace("/[^a-z0-9]/is", "", "$%#teststring34"); Link to comment https://forums.phpfreaks.com/topic/46365-easy-way-to-strip-out-non-alphanumeric-characters/#findComment-225528 Share on other sites More sharing options...
neel_basu Posted April 15, 2007 Share Posted April 15, 2007 Replace \W Link to comment https://forums.phpfreaks.com/topic/46365-easy-way-to-strip-out-non-alphanumeric-characters/#findComment-229607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.