2

I am building a gallery in WordPress and I'm trying to grab a specific part of my URL to echo into the id of a div.

This is my URL:

http://www.url.com/gallery/truck-gallery-1

I want to isolate the id of the gallery which will always be a number(in this case its 1). Then I would like to have a way to print it somewhere, maybe in the form of a function.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Carlos Rios
  • 345
  • 1
  • 3
  • 15

3 Answers3

2

You should better use $_SERVER['REQUEST_URI']. Since it is the last string in your URL, you can use the following function:

function getIdFromUrl($url) {
    return str_replace('/', '', array_pop(explode('-', $url)));
}

@Kristian 's solution will only return numbers from 0-9, but this function will return the id with any length given, as long as your ID is separated with a - sign and the last element.

So, when you call

 echo getIdFromUrl($_SERVER['REQUEST_URI']);

it will echo, in your case, 1.

Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
  • Thanks Sascha, I tried this too but the problem Im having here is that I want it to display the number only and right now Its displaying the forwardslash that is at the end of the url aswell so it looks like this (1/) – Carlos Rios Jun 29 '11 at 00:35
1

If the ID will not always be the same number of digits (if you have any ID's greater than 9) then you'll need something robust like preg_match() or using string functions to trim off everything prior to the last "-" character. I would probably do:

<?php
$parts = parse_url($_SERVER['REQUEST_URI']);
if (preg_match("/truck-gallery-(\d+)/", $parts['path'], $match)) {
    $id = $match[1];
} else {
    // no ID found!  Error handling or recovery here.
}
?>
curtisdf
  • 4,130
  • 4
  • 33
  • 42
  • Thanks Curtis! this code works, the only problem I'm having is that I have no way to echo it. I tried to echo the $id but for some reason it didnt work when I used it in a wordpress shortcode. Any suggestions as to what I should do? – Carlos Rios Jun 29 '11 at 00:24
  • I'm sorry to say I'm not familiar with Wordpress shortcode. Maybe the [Wordpress Shortcode API codex](http://codex.wordpress.org/Shortcode_API) will give you some pointers on how to incorporate my code into a function. – curtisdf Jun 29 '11 at 00:50
  • Its very new to me too. My programming skills are a very minimal, but I was thinking if I could wrap it into a function I might be able to call it using something like [nggallery - ('.$id.')] – Carlos Rios Jun 29 '11 at 00:55
0

Use the $_SERVER['REQUEST_URI'] variable to get the path (Note that this is not the same as the host variable, which returns something like http://www.yoursite.com).

Then break that up into a string and return the final character.

$path = $_SERVER['REQUEST_URI'];
$ID   = $path[strlen($path)-1];

Of course you can do other types of string manipulation to get the final character of a string. But this works.

Kristian
  • 21,204
  • 19
  • 101
  • 176
  • Use `$_SERVER["REQUEST_URI"]` instead. When using WordPress, `$_SERVER['SCRIPT_NAME']` will just give you `index.php` – Arvin Jun 27 '11 at 23:07
  • Thanks Kristian your script worked perfectly except arvin was right, it was better using $_SERVER["REQUEST_URI"] for wordpress. Thanks again guys – Carlos Rios Jun 27 '11 at 23:11
  • Thanks guys but I am going to need it to display larger numbers as well because these ID's are being used by a gallery to figure out which pages they will display what info on. And there are many galleries that I'm using. – Carlos Rios Jun 29 '11 at 00:51