asyadiqin Posted January 9, 2008 Share Posted January 9, 2008 Hi all, sorry if someone have posted this solution elsewhere but I couldn't find any when i search the forum. I need to be able to replace double slashes "//" in a url string. Example as follows http://www.mydomain.com/with//double//quotes/ to http://www.mydomain.com/with/double/quotes/ I was told that this can be done using preg_replace, but the problem is to write a regular expression to ignore and not replace the double quotes in "http://". I am not very good with regular expression and have tried but failed miserably. I am not sure if this is the correct route to perform this task but any help is greatly appreciated. Currently, as a workaround, I just explode the url string, replace the double quotes and add "http://" to the string, but I am curious to know if this can be done with just a single line of PHP code. Thanks. Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 10, 2008 Share Posted January 10, 2008 php isn't ti basic, where shorter lines of code yield more efficiency rather if possible a non-regex solution is most likely better, but shorter lines are easier on the eyes have tried but failed miserably here's a nice tut: http://www.phpfreaks.com/tutorials/52/0.php search "regex lookaheads lookbehinds" in google Pattern: ~(?<!http:)//~ Visualize it: http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=mgup3cnz Code it: $url = 'http://www.mydomain.com/with//double//quotes/'; $url = preg_replace('~(?<!http:)//~', '', $url); echo $url; Quote Link to comment Share on other sites More sharing options...
asyadiqin Posted January 10, 2008 Author Share Posted January 10, 2008 Thanks for the help. It works great and make my code looks a bit more nicer. Anyway, thanks for the tutorial link. I will look into it to further understand more about regex. Thanks again. 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.