scootstah Posted January 12, 2012 Share Posted January 12, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/254889-guess-url-for-pagination/ Share on other sites More sharing options...
scootstah Posted January 12, 2012 Author Share Posted January 12, 2012 DOH! I'm an idiot sometimes. The issue was that in the process of switching to this pattern instead of more-complicated-than-needed-to-be code, I accidentally removed the assignment of $link. Derp brb more coffee Quote Link to comment https://forums.phpfreaks.com/topic/254889-guess-url-for-pagination/#findComment-1306920 Share on other sites More sharing options...
.josh Posted January 12, 2012 Share Posted January 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254889-guess-url-for-pagination/#findComment-1306923 Share on other sites More sharing options...
scootstah Posted January 12, 2012 Author Share Posted January 12, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/254889-guess-url-for-pagination/#findComment-1306926 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.