How to determine if end of URL matches "/my-phrase"
I need to execute conditional code if the last part of the URL string is /my-phrase
How can I parse the URL for a match after the last "/" character in the开发者_StackOverflow URL string?
if(end of URL is "/my-phrase")
{ //dosomething;}
else
{//something else;}
substr($URL, -1 * strlen("/my-phrase")) == "/my-phrase"
you can explode on "/". then check the last element
$url="http://www.somewhere.com/my-phrase";
$s = explode("/",$url);
if ( end($s) == "my-phrase" ){
print "found";
}
The other answers posted so far are good, but I personally prefer the simplicity of:
if(preg_match("#/my-phrase$#", $url)) {
//...
}
精彩评论