OptimusCrime
OptimusCrime

Reputation: 14863

Very simple regex to get the numbers in url: http://artige.no/bilde/6908

This is just a really simple regex-question. I'd like to grab the last numbers from a url that looks like this:

http://artige.no/bilde/6908

The url will always look like this, with a number after /bilde/

Upvotes: 0

Views: 2963

Answers (4)

Madara's Ghost
Madara's Ghost

Reputation: 175088

In which case,

~/bilde/(\d+)~

Is what you seek. Will find any number of digits after the string /bilde/

Upvotes: 4

Jon
Jon

Reputation: 437784

You do not really need a regex for this.

$url = 'http://artige.no/bilde/6908';
$num = intval(substr($url, strrpos($url, '/') + 1));
echo $num;

Upvotes: 7

Kent
Kent

Reputation: 195239

"(?<=/bilde/)\d+$"

test with grep:

kent$  echo "http://artige.no/bilde/6908"|grep -Po "(?<=/bilde/)\d+$"
6908

as you required, (/bilde/) url like:

http://artige.no/NoTWithBilde/1234

will not be matched.

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270767

$matches = array();
$url = "http://artige.no/bilde/6908";
preg_match('/([0-9]+)$/', $url, $matches);
print_r($matches);

This uses the pattern ([0-9]+)$ to match one or more digits at the end of the string.

Upvotes: 0

Related Questions