Jump to content

Recommended Posts

I suck at regex, help me out here.

 

I have a pagination function that will build page links. If no link is given (for pages) then it will attempt to guess what it should be. It does this by assuming the end of the string is where you want the page.

 

For instance, if you are on example.com/news/somearticle, it will assume that the pages should be example.com/news/somearticle/2

 

Obviously if you are already on example.com/news/somearticle/2 it would then make it example.com/news/somearticle/2/3, which is of course wrong.

 

So I need to strip /NUMBER/ off the end of the URL. Seems simple enough, yet for some reason it is stripping the entire segments all the way back to the domain...so it looks like example.com/2

 

It works perfectly in my Regular Expressions Tester (a firefox addon), so I'm not sure what gives.

 

$link = preg_replace('~\/?\d*\/?$~', '', $link);

Link to comment
https://forums.phpfreaks.com/topic/254889-guess-url-for-pagination/
Share on other sites

Other than unnecessarily escaping your forward slashes (they mean nothing special in regex, the only time you need to escape them is if you are using them as pattern delimiter, which you are not (you are using "~")), the regex works fine.

 

example:

$links = array(
  "http://www.example.com/news/somearticle1",
  "http://www.example.com/news/somearticle2/2",
  "http://www.example.com/news/somearticle3/3",
  "http://www.example.com/news/somearticle4/44",
);

foreach ($links as &$link) {
  $link = preg_replace('~/?\d*/?$~', '', $link);
}

print_r($links);

 

output:

Array
(
    [0] => http://www.example.com/news/somearticle
    [1] => http://www.example.com/news/somearticle2
    [2] => http://www.example.com/news/somearticle3
    [3] => http://www.example.com/news/somearticle4
)

 

So your problem is somewhere else in your code.  Maybe you are preg_replacing the wrong variable or assigning results to wrong variable (like a typo or something).  Maybe later on you use the wrong variable to add new number on. Who knows, I'm just throwing out random guesses since you have no other code posted.

Other than unnecessarily escaping your forward slashes (they mean nothing special in regex, the only time you need to escape them is if you are using them as pattern delimiter, which you are not (you are using "~")), the regex works fine.

 

Thanks, forgot to remove that after switching the delimiter.

 

So your problem is somewhere else in your code. 

 

Yeah, I had a major derp moment (see my post above). :P

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.