xnowandtheworldx Posted July 28, 2008 Share Posted July 28, 2008 Basically, I have a script where it takes a users input, and will replace !@#$% etc. etc. with an _. Now I need a regex expression that will remove all the extra _'s say for instance.... !hello*#world(*! would then be turned into "_hello__world___", now what I need to do is take that output and turn it into hello_world. Is there any possible way to do this with regex? I suck with regex, so any help would be nice! Thanks for any future help some of you may provide. Quote Link to comment Share on other sites More sharing options...
effigy Posted July 28, 2008 Share Posted July 28, 2008 Have you original regex match multiple characters. Quote Link to comment Share on other sites More sharing options...
xnowandtheworldx Posted July 28, 2008 Author Share Posted July 28, 2008 I'm using str_replace() currently to replace all the special symbols. So how might I go about to check for multiple instances of the _'s with regex? Thank you for any help. Quote Link to comment Share on other sites More sharing options...
corbin Posted July 28, 2008 Share Posted July 28, 2008 preg_replace('/[_]+/', '', $str); Or, just: preg_replace('/[!@#$%]+/', '_', $str); Quote Link to comment Share on other sites More sharing options...
xnowandtheworldx Posted July 28, 2008 Author Share Posted July 28, 2008 alright, i've got it fixed. Heres what i used. $string = preg_replace( "/\_{1,}/", "_", $string); thanks for all your help guys! Quote Link to comment Share on other sites More sharing options...
effigy Posted July 28, 2008 Share Posted July 28, 2008 Why run two replacements when you can do it once with the second expression? Quote Link to comment Share on other sites More sharing options...
xnowandtheworldx Posted July 28, 2008 Author Share Posted July 28, 2008 Even if I did that expression wouldn't i still have to remove the extra underscores? basically, what i needed was a regex command that would remove the _'s from the beginning and the end, and any double _'s there may be. Plus I don't know how to do a regex command for all the symbols...i need it for symbols !@#$%^&*()-+={[}]|\;;"'<,>.?/ Quote Link to comment Share on other sites More sharing options...
effigy Posted July 29, 2008 Share Posted July 29, 2008 Try this: <pre> <?php echo $str = 'ABC!@#$%^&*()-+={[}]|\;;"\'<,>.?/DEF'; echo '<hr>'; echo preg_replace('/[\p{P}\p{S}]+/', '_', $str); ?> </pre> Quote Link to comment 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.