The regex below takes the number (company id) out from the URL below, but I'm trying to find out how it works. What does hash to and pluses do?
preg_match('#/company/([0-9]+)/(.+)#',http://test.net.au/company/198/test,$m);
The regex below takes the number (company id) out from the URL below, but I'm trying to find out how it works. What does hash to and pluses do?
preg_match('#/company/([0-9]+)/(.+)#',http://test.net.au/company/198/test,$m);
Hash here is a regular expression delimiter. You can use almost anything as a delimiter, like ~
, !
, etc.
+
is a quantifier that means repetition from 1 to infinity.
[0-9]+ --- 1 or more numbers in a row
.+ --- any character one or more times in a row
The first and last characters in the regex are delimiters. They are there just to say here is where the regex starts and here is where it stops:
Usually /
is used:
Example: "/foo/";
"#bar#";
In your example you are trying to match "/company/"
so the delimiter can't be a /
so #
is used instead.
The plus +
means match 1 or more of the previous entry. Meaning in your case it will match one or more digits (From 0 to 9)
The hashes are just used as the regex pattern's bounding delimiters. Commonly developers use /
, ~
, or #
for this. In this case #
was used to prevent the need to escape the /
's in the regex pattern.
The +
indicates that there must be one or more of the preceding element, which in the first case is actually a character class specifying all digits. In the second case it just means there need to be one or more of any character (.
is wildcard).