tags:

views:

58

answers:

2

Hi,

I would like to isolate a certain part of a URL if the structure remains the same:

ex. URL - http://www.URL.co.uk/87/318/carrot%5Fcake/

it is the 318 part of the example above, however the number can range between 1 - 999999 - but will always remain between 2 x '/' and to the left of the title (carrot_cake in the example) this is where I strugling.

Thanks, B

+5  A: 

This should work:

<?php
$url = 'http://www.URL.co.uk/87/318/carrot%5Fcake/';
$id = explode('/', $url);
echo $id[5];
?>

Update: fixed code, and tested

moff
@soulmerge ....... can u elaborate on that a bit more .. i think it should work just fine!
Sabeen Malik
thanks...small comment tho - $id[5]; workedfor me.
Bift
Your welcome :) And I've corrected the code now.
moff
+2  A: 

If Moff's answer is what you need, use it as it is simpler. If it's not, does this get help?

<?php
  if (preg_match  ('/^//\d+//(\d+)//.*/', parse_url($url, PHP_URL_PATH), $results)) {
 print_r($results);  
  }
?>

(My local PHP install is broken right now so I'm hoping that the syntax is correct.)

Ben Gribaudo
I use codepad.org for this sort of thing when on the go.
Sam152