-1

I need to take a unique parameter from url generally appendened as last word of the url.

For example, I need to take 2220193 and 2220136 from the following urls:

  • "http://www.break.com/index/strange-sea-creature-retreats-into-ocean-floor-2220193"
  • "http://www.break.com/index/bully-picks-fight-with-sleeping-inmate-2220136"

Generally I use str_replace to remove the static parameter "http://www.break.com/index/", but I don't know how to delete the textual url and get only the number.

Pigi
  • 74
  • 8
  • *(reference)* http://php.net/strings – Gordon Nov 14 '11 at 17:25
  • possible duplicate of http://stackoverflow.com/questions/1150559/regular-expression-to-collect-everything-after-the-last and http://stackoverflow.com/questions/2762778/grab-remaining-text-after-last-in-a-php-string and countless others. please do research before asking. – Gordon Nov 14 '11 at 17:33

4 Answers4

1
$id = substr($url, strrpos($url, '-') + 1);
Eugene
  • 3,280
  • 21
  • 17
1

Use explode on the url, this returns a string array. The last value in the string array should contain what you are looking for.

Given that $_SERVER['PATH_INFO'] would contain a url of the given format, you could use.

$url = $_SERVER['PATH_INFO'];
$urlParts = explode($url, '-');

echo end($urlParts);
Jose Vega
  • 10,128
  • 7
  • 40
  • 57
  • thanks, this will delete just (-) or all the "strange-sea-creature-retreats-into-ocean-floor", and this code can be used dinamically or just for the first url? – Pigi Nov 14 '11 at 17:28
0

Probably You could use preg_match PHP function

Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68
-1
$outarr = explode('-',$inputstr);
$outnum = $outarr[count($outarr);
COBOLdinosaur
  • 284
  • 2
  • 6