Very simply:
$id = substr($url, strrpos($url, '/') + 1);
strrpos gets the position of the last occurrence of the slash; substr returns everything after that position.
As mentioned by redanimalwar if there is no slash this doesn't work correctly since strrpos
returns false. Here's a more robust version:
$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);
from os.path import basename
$str = basename($url);
You could explode based on "/", and return the last entry:
print end( explode( "/", "http://www.vimeo.com/1234567" ) );
That's based on blowing the string apart, something that isn't necessary if you know the pattern of the string itself will not soon be changing. You could, alternatively, use a regular expression to locate that value at the end of the string:
$url = "http://www.vimeo.com/1234567";
if ( preg_match( "/\d+$/", $url, $matches ) ) {
print $matches[0];
}
You can use substr
and strrchr
:
$url = 'http://www.vimeo.com/1234567';
$str = substr(strrchr($url, '/'), 1);
echo $str; // Output: 1234567
$str = "http://www.vimeo.com/1234567";
$s = explode("/",$str);
print end($s);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With