Welcome to WebmasterWorld Guest from 54.167.5.15

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to Use Part of HREF as a PHP Variable

A simpe task I'm failing miserably on

     

jimh009

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

10+ Year Member



Hello,

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 :

mydomain.com/directory/bs-3.php
mydomain.com/directory/bs-4.php
mydomain.com/directory/bs-5.php

etc....

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.

Jim

penders

11:50 am on Jul 1, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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)

Readie

12:06 pm on Jul 7, 2014 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Allow any number of hyphens:


preg_match('~\-(\d+)\.[a-z]+$~i', $_SERVER['REQUEST_URI'], $matches);
echo $matches[1];

lucy24

3:59 pm on Jul 7, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Viewed purely as a Regular Expression, the pattern is

-(\d\d?)\.php

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.

penders

5:25 pm on Jul 7, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



preg_match('~\-(\d+)\.[a-z]+$~i'


Does the hyphen need to be escaped here (outside of a character class)?

Readie

11:00 am on Jul 15, 2014 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.

penders

2:57 pm on Jul 15, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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?)

lucy24

6:55 pm on Jul 15, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month