Get characters after last / in url

后端 未结 8 915
梦谈多话
梦谈多话 2020-11-30 20:05

I want to get the characters after the last / in an url like http://www.vimeo.com/1234567

How do I do with php?

8条回答
  •  生来不讨喜
    2020-11-30 20:25

    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];
    }
    

提交回复
热议问题