homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

How to Use Part of HREF as a PHP Variable
A simpe task I'm failing miserably on

 10:52 am on Jul 1, 2014 (gmt 0)


My knowledge of PHP is very limited, and after reading way too many articles on Parse_URL, I'm probably hopelessly lost. So was hoping someone could point me in the right direction.

On my site, certain pages (all located in the same directory) all have a file extension name that goes something like this :



The only thing in the file name that will change is the number after the "-" and before the .php. The number in the file name will range from 1 up to 99.

What I am trying to do is to (1) parse the url and grab the number portion of the filename, and (2) Use that number as a variable for other PHP functions on the page. In particular, the number will be assigned to a $thisPage variable. I then use $thisPage for previous/next page navigation through my photo galleries.

I currently assign the number for $thisPage manually, which is kind of a drag. Since this is the 21st century and because the $thisPage variable will always match the number used in the file name, I figured there was bound to be a way to use PHP to do this task!

Thanks for any help.




 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[1]) ? (int)$urlParts[1] : 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);
echo $matches[1];


 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)

some say

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved