| 11:50 am on Jul 1, 2014 (gmt 0)|
I wouldn't treat it as a "URL", since you aren't doing anything URL-like with it. Just treat it as an ordinary string and manipulate it.
As always, there are many ways to do this. Here is one way...
This assumes that "-" (hyphen) only occurs (at most) once in the URL. The string is converted into an array (split on the hyphen). The second element of the array (eg. "3.php") is then converted to an integer (which effectively strips everything after the numeric part of the string). If there is no hyphen in the URL then $thisPage will be NULL. If there is no number after the hyphen then $thisPage will be 0 (zero).
$url = 'mydomain.com/directory/bs-3.php';
$urlParts = explode('-',$url);
$thisPage = isset($urlParts) ? (int)$urlParts : null;
echo $thisPage; // 3 (number)
| 12:06 pm on Jul 7, 2014 (gmt 0)|
Allow any number of hyphens:
preg_match('~\-(\d+)\.[a-z]+$~i', $_SERVER['REQUEST_URI'], $matches);
| 3:59 pm on Jul 7, 2014 (gmt 0)|
Viewed purely as a Regular Expression, the pattern is
though you can certainly say \d+ if you look forward to expanding into >100 pages. The rest of the string doesn't matter, since you're not doing anything with it.
| 5:25 pm on Jul 7, 2014 (gmt 0)|
Does the hyphen need to be escaped here (outside of a character class)?
| 11:00 am on Jul 15, 2014 (gmt 0)|
|Does the hyphen need to be escaped here (outside of a character class)? |
Dunno actually :/ I do it just to be safe and it's become habit. At absolute worst it has no effect.
| 2:57 pm on Jul 15, 2014 (gmt 0)|
AFAIK the hyphen does not have any special meaning outside of a character class, so it shouldn't need to be escaped here. (Even when inside of a char class it can be placed at the start (and some say end) of a class to negate its special meaning - so, I don't think it ever really needs to be escaped?)
| 6:55 pm on Jul 15, 2014 (gmt 0)|
It depends on the RegEx engine. Different applications use the underlying language in different ways. If you're using something new, it's worth doing a meta-test: does escaping something that doesn't need to be escaped have any effect? For example, if \a has no special meaning then check whether \a is interpreted as a.