amb99 Posted February 2, 2007 Share Posted February 2, 2007 Hi, I've read quite a bit and from my understanding I can check if the string is alphanumeric (and _-) by doing: if (preg_match('/^[a-zA-Z0-9_.]+$/', $string)) { echo "invalid chars."; } But, how can I return the string with every illegal character stripped out? (with the exception of _-) Thank you for any help. Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/ Share on other sites More sharing options...
HuggieBear Posted February 2, 2007 Share Posted February 2, 2007 I'd take a look at the function preg_replace() See how you get on with that one and then post back with any problems. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/#findComment-175257 Share on other sites More sharing options...
amb99 Posted February 2, 2007 Author Share Posted February 2, 2007 That seems to be correct.. however I've tried doing: $pattern = '/^[A-Za-z0-9_.]+$/'; $replacement = ''; $out = preg_replace($pattern, $replacement, $string); echo "out: $out"; The result $out isn't getting stripped of any illegal chars though. I thought it mightve been related to $ in /^[A-Za-z0-9_.]+$/', so as a test I changed it to /^[A-Za-z0-9_.]/', but still no luck. Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/#findComment-175264 Share on other sites More sharing options...
kucing Posted February 2, 2007 Share Posted February 2, 2007 What do you mean by "illegal chars"? Any example? Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/#findComment-175271 Share on other sites More sharing options...
alpine Posted February 2, 2007 Share Posted February 2, 2007 Here is a non-regex version: <?php $string = "gsjdtb7l,-'%&!"; echo str_replace(array_values(str_replace(array_merge(range(a,z),range(A,Z),range(0,9),array('-','.')),'',str_split($string))),'',$string); // prints: gsjdtb7l- ?> EDIT* Requires php5 though due to str_split() Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/#findComment-175282 Share on other sites More sharing options...
HuggieBear Posted February 2, 2007 Share Posted February 2, 2007 Try this... <?php $pattern = '/[^\w\.]/i'; $replacement = ''; $out = preg_replace($pattern, $replacement, $string); echo "out: $out"; ?> This says replace anything that's not a word character (letters a-z, numbers 0-9, and the underscore) or a period (.) with nothing. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/#findComment-175285 Share on other sites More sharing options...
amb99 Posted February 2, 2007 Author Share Posted February 2, 2007 alpine: I'm using 4.X so I was unfortunately unable to test that method. HuggieBear: That worked wonderfully! Thank you very very much. Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/#findComment-175293 Share on other sites More sharing options...
HuggieBear Posted February 2, 2007 Share Posted February 2, 2007 alpine: I'm using 4.X so I was unfortunately unable to test that method. HuggieBear: That worked wonderfully! No problem, your code was fine, it was missing one important character though. The circumflex (^) at the start of the character class that negates it. Basically says, match anything that's NOT one of the following characters. I just used the word meta character (\w) to simplify what you'd written. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36749-solved-how-to-strip-illegal-chars-by-regex/#findComment-175294 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.