AHA7 Posted May 23, 2007 Share Posted May 23, 2007 Hello, I want to search a string for a .com URL which may contain a trailing slash. If it does contain a trailing slash I want to strip it out using preg_replace. If it doesn't contain a trailing slash then I want to keep it as is and in both cases I want to store the no-trailing-slash-URL in the variable $URL_with_trailing_slash_stripped. (I do not want to affect the original string). Here's my solution: preg_match('/http://.*\.com[/]?/', $string, $matches); $URL_with_trailing_slash_stripped = preg_replace('//$/', '', $matches[0]); And here are my two questions: 1- Is the second line of the code above valid? Does preg_replace return a string and so I can store it in a variable? If not, how can I convert it to a string and store it in a variable? 2- Do I have to wrap the regex with forward slashes '//$/' (I have seen people do so and I don't know why)? Quote Link to comment https://forums.phpfreaks.com/topic/52618-preg_match-and-preg_replace/ Share on other sites More sharing options...
MadTechie Posted May 23, 2007 Share Posted May 23, 2007 replace the / (delimiters).. with something else.. IE preg_replace('#/$#', '', $matches[0]); can you give some example of what you have and want ie /test//data//123/ to /test/data/123/ but //test//data//123// stays as is //test//data//123// thats just an example.. Quote Link to comment https://forums.phpfreaks.com/topic/52618-preg_match-and-preg_replace/#findComment-259671 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.