Jump to content

urlSegment regex year-year


cb2004
 Share

Recommended Posts

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 by netcarver
I even got my regex wrong!
  • Like 5
Link to comment
Share on other sites

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).

  • Like 1
Link to comment
Share on other sites

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 by Sérgio
Link to comment
Share on other sites

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 :)

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...