Jeffro Posted February 21, 2012 Share Posted February 21, 2012 For all my urls, I'm needing to replace all numbers, letters and underscores encountered at the very end of all my urls with the word "title" The /ref= will always be at the end of the url (representing the last forward slash in the url) and there are always either (and only) numbers, underscores or letters after the = sign. Example: $myurl = "http:ljlkjlkjlkjl.com/category/date/ref=123_abc_de" would be: http:ljlkjlkjlkjl.com/category/date/title Thanks for any help. I still just have the hardest time with regex. Quote Link to comment https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/ Share on other sites More sharing options...
requinix Posted February 21, 2012 Share Posted February 21, 2012 Substituting that whole segment? You can just find the "/ref=" and grab everything else after it - don't need any "numbers, letters, and underscores" logic. #/ref=.*# Quote Link to comment https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/#findComment-1319443 Share on other sites More sharing options...
Jeffro Posted February 21, 2012 Author Share Posted February 21, 2012 Substituting that whole segment? You can just find the "/ref=" and grab everything else after it - don't need any "numbers, letters, and underscores" logic. #/ref=.*# Wow.. much easier than I had thought it would be. Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/#findComment-1319449 Share on other sites More sharing options...
AyKay47 Posted February 21, 2012 Share Posted February 21, 2012 Depending on where the data is coming from, I would use this instead. #/ref=\w*# this will eliminate invalid characters. Again, this will depend on where the urls are coming from. Quote Link to comment https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/#findComment-1319517 Share on other sites More sharing options...
Adam Posted February 21, 2012 Share Posted February 21, 2012 this will eliminate invalid characters. Again, this will depend on where the urls are coming from. [...] and there are always either (and only) numbers, underscores or letters after the = sign. There's no sense in slowing down the regex if there will only ever be numbers, letters and underscores. Quote Link to comment https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/#findComment-1319568 Share on other sites More sharing options...
xyph Posted February 21, 2012 Share Posted February 21, 2012 Assuming you're going to use the data later, a more accurate RegEx could also provide necessary sanitation later. Something like #/ref=[_a-z0-9]+$#i would be a great idea if the data was going to be used in a query later, or output to the browser at any point. If you wanted to support multiple variables in the header, hashtags etc. %(?:/|&)ref=([_a-z0-9]++)(?:$|&|#)%i WORKS /ref=zomg_123 Full match: /ref=zomg_123 Capturing group: zomg_123 FAILS /ref=injectable"characters WORKS /ref=foo&other=bar Full match: /ref=foo& Capturing group: foo WORKS /foo=bar&ref=another#hashtag Full match: &ref=another# Capturing group: another Quote Link to comment https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/#findComment-1319681 Share on other sites More sharing options...
AyKay47 Posted February 21, 2012 Share Posted February 21, 2012 this will eliminate invalid characters. Again, this will depend on where the urls are coming from. [...] and there are always either (and only) numbers, underscores or letters after the = sign. There's no sense in slowing down the regex if there will only ever be numbers, letters and underscores. it won't slow anything down and will provide a tighter pattern. Again I will state, it depends on where the url is coming from. If it is coming from an unknown source of some sort., using the dot to match is not okay. Now I know that the OP said that those are the only values that can be in the results, but I am skeptical. xyph has also stated the sanitation usage of a tighter pattern. Quote Link to comment https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/#findComment-1319721 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.