开发者

How to determine if end of URL matches "/my-phrase"

开发者 https://www.devze.com 2022-12-21 10:47 出处:网络
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?

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)) {
  //...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号