Jump to content

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/257436-how-to-replace-ref-in-my-url/
Share on other sites

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.