cb2004 Posted September 29, 2016 Share Posted September 29, 2016 I must admit, regex is certainly not my strong point. What would I have to put in to allow for an urlSegment of say 2015-2016? Thanks people. Link to comment Share on other sites More sharing options...
cb2004 Posted September 29, 2016 Author Share Posted September 29, 2016 I rock at regex regex:^[0-9-]+$ 1 Link to comment Share on other sites More sharing options...
netcarver Posted September 29, 2016 Share Posted September 29, 2016 (edited) Hi @cb2004 That's a fine start However, it will also match "-", "--", "---", "-2000", "-0", "-1000-1", "1---", "31415926" etc. If you are expecting a 4 digit start and end year separated with a single dash then you will need to use a more restrictive/specific regular expression. Assuming the start year is in the 1900 to 2099 range, and the end year in the 1900 to 2999 range, you'd need something more like this; Quote ^(19|20)[0-9]{2}-(19|2[0-9])[0-9]{2}$ Also, if you are going to be splitting this string up into the start and end year, you might want to use a urlSegment for the start and a separate one for the end year. If that is the case, you'd use ^(19|20)[0-9]{2}$ for the start urlSegment and ^(19|2[0-9])[0-9]{2}$ for the end urlSegment. Hope that helps! Edited September 29, 2016 by netcarver I even got my regex wrong! 5 Link to comment Share on other sites More sharing options...
adrian Posted September 29, 2016 Share Posted September 29, 2016 Here's a nice article with some examples if you need to allow dates before 1900 http://www.x-note.co.uk/javascript-regex-validate-years-in-range/ Also don't forget about some of the great regex testing tools: https://leaverou.github.io/regexplained/https://regex101.com/http://www.phpliveregex.com/ 6 Link to comment Share on other sites More sharing options...
netcarver Posted September 29, 2016 Share Posted September 29, 2016 I agree with Adrian: the regex101 site is very useful! 2 Link to comment Share on other sites More sharing options...
Sergio Posted September 30, 2016 Share Posted September 30, 2016 Why not a simpler one, like: ^\d{4}-\d{4}$ ? Does anybody see a problem with this approach? Link to comment Share on other sites More sharing options...
adrian Posted September 30, 2016 Share Posted September 30, 2016 1 minute ago, Sérgio said: Why not a simpler one, like: ^\d{4}-\d{4}$ ? Does anybody see a problem with this approach? Probably nothing wrong - I guess it depends on how determined you are to make sure they are valid years. Yes 9155 is a valid year, but it if was an accidental transposition of the 9 and 1, then it might be nice to exclude it if you are only expecting years plus or minus a hundred of today (or whatever is appropriate). 1 Link to comment Share on other sites More sharing options...
Sergio Posted September 30, 2016 Share Posted September 30, 2016 (edited) 5 minutes ago, adrian said: Probably nothing wrong - I guess it depends on how determined you are to make sure they are valid years. Yes 9155 is a valid year, but it if was an accidental transposition of the 9 and 1, then it might be nice to exclude it if you are only expecting years plus or minus a hundred of today (or whatever is appropriate). Agree, but wouldn't be better to check this after the regex has matched, something like this, for instance: $year_url_segment = $sanitizer->selectorValue($input->urlSegment); $years_on_database = range(date("Y"),2007); //Range of years from 2007 to present if (in_array($year_url_segment, $years_on_database) ) { //find pages in that year } Off course I'm considering the url with just one year, not a period. Edited September 30, 2016 by Sérgio Link to comment Share on other sites More sharing options...
adrian Posted September 30, 2016 Share Posted September 30, 2016 3 minutes ago, Sérgio said: Agree, but wouldn't be better to check this after the regex has matched, something like this, for instance: $year_url_segment = $sanitizer->selectorValue($input->urlSegment); $years_on_database = range(date("Y"),2007); //Range of years from 2007 to present if (in_array($year_url_segment, $years_on_database) ) { //find pages in that year } Off course I'm considering the url with just one year, not a period. Yeah, maybe that would be more efficient, but wouldn't you also want to throw a 404 if it didn't match match your required range? Using a more specific regex in the "Which URL segments do you want to allow?" would take care of this automatically. I am no expert in the efficiencies of regex matching, but do you think it's worth having a simpler regex to save the additional checks in the code? I guess you'd need to test the two and see 1 Link to comment Share on other sites More sharing options...
netcarver Posted September 30, 2016 Share Posted September 30, 2016 @Sérgio There's nothing wrong with that approach, \d{4} should work fine. I guess it also comes down to where you want to codify the rules - in the regex, or in the PHP, or some combination of both. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 30, 2016 Share Posted September 30, 2016 I think i'd probably go with this in the regex (if it's not for historical data) (1|2)\d{3}-(1|2)\d{3} For something like a blog there's hardly need for years before 1000 and beyond 2999 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now