I am trying to match a URL such as;
http://www.testing.com/documents/dashboard
What I want is to match “/documents/dashboard” or “/documents/dashboard/”. How can be this done?
preg_match('/^\/documents\/dashboard[\/|]$/i', $_SERVER['REQUEST_URI']);
This doesn’t work? What should I enter after pipe (|) character in [] block to cover “nothing” as well?
[\/|]$is wrong, because[]creates a character class. So what you are matching there is/or|followed by end of string. To do what you were thinking of:Although I think it’s easier to use:
/?means match the/character 1 or 0 times.Tip: If you use a delimiter other than
/, you won’t have to escape forward slashes in the pattern.