Search code examples
phpregexpreg-match

Redirect the Url pattern ending with specialcharacter and number


My requirement is to redirect some of my urls which contains a 8 digit number and a special character at the end.

Example urls are given below

www.example.com/anything-here/hello-12345677

www.example.com/anything-here/again-here/again-hello-12543598

www.example.com/anything-here/hello-12345677

I am using pregmatch in php to find the patterns containing "-" and 8 digit number.

I tried the below code and it is not working for me. Is my pattern correct?

$result = preg_match("#.*/-\d{8}$#i", request_uri());

Solution

  • The following pattern will match any URL with a dash followed by 8 digits at the end. URLs with a special character at the end would not match.

    $result = preg_match("/.*-\d{8}$/i", request_uri());
    

    Demo here:

    Regex101