I need to extract a piece of a URL in PHP. From the URL http://www.flickr.com/photos/28370443@N08/7885410582
I need only the 7885410582
part.
My code:
$string2 = 'http://www.flickr.com/photos/28370443@N08/7885410582';
preg_match_all('~s/ (.*?) /~i',$string2,$matches, PREG_PATTERN_ORDER);
echo $matches[1][0] . '<br />';
Can anyone look at it and correct it for me? Thanks in advance.
try this regex:
\d+$
will find all digits before string end
or:
(?<=/)\d+$
will find all digits after /
sign before string end
If the identifier you want is always the last in the URL, don't bother with regular expressions and instead use simple string functions.
<?php
$url = 'http://www.flickr.com/photos/28370443@N08/7885410582';
$flickr_id = substr($url, strrpos($url, "/") + 1);
echo $flickr_id; // 7885410582
Try this:
preg_match_all('~^.+/(\d+)$~',$string2,$matches);
$string2 = 'http://www.flickr.com/photos/28370443@N08/7885410582';
preg_match('~^.+/(\d+)$~',$string2,$matches);
echo $matches[1]; // 7885410582
you can use
$data = explode("/",$url)
$value = $data[sizeof($data)-1]