Michan Posted November 20, 2007 Share Posted November 20, 2007 Hi everyone, I have a string, and would like to convert it to just letters and numbers. I've seen ways of achieving close results, but not yet of the one described below. Let's say $string outputs the following: "Mario Galaxy" is, without doubt, the Wii's #1 game. I'd like $string to strip anything that isn't a character between a - z or 0 -9, and end up looking somewhat like the following; MarioGalaxyiswithoutdoubttheWiis1game How would I get about doing this? Many thanks in advance! - Mi Link to comment https://forums.phpfreaks.com/topic/78050-solved-remove-anything-but-a-z-0-9-from-a-string/ Share on other sites More sharing options...
pranav_kavi Posted November 20, 2007 Share Posted November 20, 2007 try, <?php $string_to_be_stripped = "Hi, I am a string and I need to be stripped..."; $new_string = ereg_replace("[^A-Za-z0-9]", "", $string_to_be_stripped ); ?> Link to comment https://forums.phpfreaks.com/topic/78050-solved-remove-anything-but-a-z-0-9-from-a-string/#findComment-395033 Share on other sites More sharing options...
Wuhtzu Posted November 20, 2007 Share Posted November 20, 2007 This will do the job: <?php //I had to escape your string since it contained " and ' $string = '"Mario Galaxy" is, without doubt, the Wii\'s #1 game.'; $new_string = preg_replace("/[^a-z0-9]/i", '', $string); echo $new_string; ?> Link to comment https://forums.phpfreaks.com/topic/78050-solved-remove-anything-but-a-z-0-9-from-a-string/#findComment-395035 Share on other sites More sharing options...
Michan Posted November 20, 2007 Author Share Posted November 20, 2007 Thank you, both methods work perfectly Solved! Link to comment https://forums.phpfreaks.com/topic/78050-solved-remove-anything-but-a-z-0-9-from-a-string/#findComment-395046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.