Amir
Amir

Reputation: 29

Extract whole number between 2 slashes

I'm trying to extract id, which is a whole number from a url, for example:

http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a

I'd like to extract only 106 from the string in PHP. The only dynamic variables in the URL would be the domain and the hash after 106/ The domain, id, and hash after 106/ are all dynamic.

I've tried preg_match_all('!\d+!', $url, $result), but it matches all number in string.

Any tips?

Upvotes: 0

Views: 49

Answers (2)

Vaibhavi S.
Vaibhavi S.

Reputation: 1093

Try this. This will works.

$url = $_SERVER['REQUEST_URI']; //"http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
$str = explode('/',$url);
echo "<pre>";
print_r($str);

$id = $str[5]; // 106

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522032

The pattern \b\d+\b might be specific enough here:

$url = "http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
preg_match_all('/\b\d+\b/', $url, $matches);
print_r($matches[0]);

This prints:

Array
(
    [0] => 106
)

Upvotes: 1

Related Questions