Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract/Pull numerical ID from string

Status
Not open for further replies.

Bravogolf

Programmer
Nov 29, 2002
204
GB
Hi all, I'm stumped on this one! I have a collection or URL's entered by the user and I want to grab the numerical value contained within the URL. The URL will always appear as and I'll always want to grab the numbers part (but the length of numbers may vary as they're incremented). Any help is appeciated as always!
 
<?php
function getInfo(){
$url = rtrim($_SERVER['REQUEST_URI'], '/');
$urlParts = explode('/', $url);
$lastPart = end($urlParts);
/*
if the pattern is always two letters followed by an arbitrary amount of numbers
then you do not need to use preg_match and you can instead use this
return substr($lastPart, 2);
otherwise:
*/
if(preg_match('/([0-9]+)/', $lastPart, $matches)){
return $matches[1];
} else {
return false;
}
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top