scanreg Posted September 13, 2010 Share Posted September 13, 2010 I found this for eliminating trailing and back slashes: $clean = ereg_replace ("^[\/]+", "", $not_clean); Doesn't the back slash need to be escaped, like this: $clean = ereg_replace ("^[\\/]+", "", $not_clean); Thanks : ) Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/ Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2010 Share Posted September 13, 2010 What happened when you tried it? Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/#findComment-1110461 Share on other sites More sharing options...
cags Posted September 13, 2010 Share Posted September 13, 2010 1.) You should consider using preg_replace instead of ereg_replace as the latter is deprecated. 2.) Your pattern matches 1 or more capital V characters anchored to the start of the string and replaces them with noting. 3.) \V would mean any character that is not a vertical whitespace character. So essentially it would match anything that is not a newline character (making it very similar, if not the same as using .). Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/#findComment-1110497 Share on other sites More sharing options...
scanreg Posted September 13, 2010 Author Share Posted September 13, 2010 It looks like a V but it's really a back slash and a forward slash: \ / I didn't want to put a space in there Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/#findComment-1110531 Share on other sites More sharing options...
cags Posted September 13, 2010 Share Posted September 13, 2010 Oh, my bad. Well if the objective is to match any forward slashes at the start of the string you don't need either backslash as the forward slash character has no special meaning. The only reason to escape it is if you are using PCRE and have used it as a delimiter. But personally I'd suggest using a different delimiter, since the forward slash is all too common in patterns that involve paths or HTML. Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/#findComment-1110547 Share on other sites More sharing options...
scanreg Posted September 13, 2010 Author Share Posted September 13, 2010 but what if I'm trying to match both forward slashes and back slashes: $clean = ereg_replace ("^[\ /]+", "", $not_clean); (added a space in between for clarity) should it be like this: $clean = ereg_replace ("^[\\ /]+", "", $not_clean); (added space here too) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/#findComment-1110656 Share on other sites More sharing options...
Psycho Posted September 13, 2010 Share Posted September 13, 2010 $clean = preg_replace("#^(\\\|/)*(.*?)(\\\|/)*$#", '${2}', $not_clean); Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/#findComment-1110675 Share on other sites More sharing options...
scanreg Posted September 14, 2010 Author Share Posted September 14, 2010 $clean = preg_replace("#^(\\\|/)*(.*?)(\\\|/)*$#", '${2}', $not_clean); Ah, you have to escape the escape for the escape to work, okay, got it Thanks for the super nice preg_replace too, neat Quote Link to comment https://forums.phpfreaks.com/topic/213260-eliminate-trailing-and-back-slashes-escapes/#findComment-1111004 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.