poe Posted May 24, 2007 Share Posted May 24, 2007 how do i remove NON alphanumeric characters from a string including spaces. example : if i have a string like: $mystring = " hEllo, my_fav-Number^is&256! "; i want result to be : hEllomyfavNumberis256 thanks chris Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/ Share on other sites More sharing options...
corbin Posted May 24, 2007 Share Posted May 24, 2007 Regular expressions would be the easiest answer here... $intput = preg_replace("/[^a-z0-9]/i", "", $input); Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261110 Share on other sites More sharing options...
BrandonE97 Posted May 24, 2007 Share Posted May 24, 2007 Would that be a good replacement for the addslashes function if the person wanted to use it that way? Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261115 Share on other sites More sharing options...
corbin Posted May 24, 2007 Share Posted May 24, 2007 Well, if you're putting data into a a database, you don't exactly have to worry about anything alpha numeric.... So in theory if you replaced anything not alphanumeric with "", then you would be perfectly safe.... The only problem is, when inserted data, you wouldn't get the entire data so you would want to check against the original value and make sure the user won't get some weird error.... For example, if you had a login script and two users made names.... One could make "phpguy" and another could make "php guy" and all the sudden MySQL is pulling two rows for that name because the replace would rip the space. Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261124 Share on other sites More sharing options...
BrandonE97 Posted May 24, 2007 Share Posted May 24, 2007 How would you use it to leave all the alphanumeric and only periods and no other symbols? Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261135 Share on other sites More sharing options...
MadTechie Posted May 24, 2007 Share Posted May 24, 2007 <?php $input = preg_replace("/[^a-z0-9.]/i", "", $input); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261140 Share on other sites More sharing options...
BrandonE97 Posted May 24, 2007 Share Posted May 24, 2007 Thanks bunches. I've hadnt really got the hang of regular expressions yet. Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261142 Share on other sites More sharing options...
MadTechie Posted May 24, 2007 Share Posted May 24, 2007 I'm Still learning.. Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261143 Share on other sites More sharing options...
BrandonE97 Posted May 24, 2007 Share Posted May 24, 2007 Is there a way to include spaces? If this uses the ASCII Table then the table has Space in it. And dashes if possible. Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261149 Share on other sites More sharing options...
MadTechie Posted May 24, 2007 Share Posted May 24, 2007 OK this is for spaces, linebreaks, tabs, etc (white spaces) letters, numbers, dots, commas, dashes <?php $input = preg_replace("/[^.\s,a-z0-9-]/im", "", $input); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-261168 Share on other sites More sharing options...
BrandonE97 Posted May 26, 2007 Share Posted May 26, 2007 Thanks bunches, this is a big help to me. Quote Link to comment https://forums.phpfreaks.com/topic/52880-remove-non-alphanumeric-characters-from-a-string/#findComment-262215 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.