tags:

views:

38

answers:

3

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 URL string?

if(end of URL is "/my-phrase")
{ //dosomething;}
else
{//something else;}
+3  A: 
substr($URL, -1 * strlen("/my-phrase")) == "/my-phrase"
pr1001
I know this worked when I last checked it, but for some reason I can't get it to work now. echo($URL) is not returning a value. Is $URL a variable or do I have to define it?
Scott B
$URL is a variable you already have with the url you want to check.
pr1001
A: 

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";
}
ghostdog74
A: 

The other answers posted so far are good, but I personally prefer the simplicity of:

if(preg_match("#/my-phrase$#", $url)) {
  //...
}
Frank Farmer