Hi, I have a script which needs to know if a certian term is in the URL. I used $_SERVER['REQUEST_URI'] to get the URL, but this is adding higher directories, and I don't want this as it makes the $_SERVER['REQUEST_URI'] different to my string. PHP SCRIPT: <?php $page = $_SERVER['REQUEST_URI']; if ($page=="/series1/episode2.php"); { $url='episode2'; } ?> Code (markup): 2 possible options to solve this: Cutting out the unwanted part of the URL - Using the $_SERVER['REQUEST_URI'] function outputs /Final%20WTIO/series1/episode2.php, where I only want it to output //series1/episode2.php[/i]. Is there a way to cut out the higher directories? Option 2 - Using an 'in string function'. Is there a function that looks in the string and finds a certain (predetermined) phrase? This would be used, ie. Thanks for your help, if you understand the longwinded problem
This should be the fastest way to check. There are other function like preg_match but strpos is the fastest. if(strpos($_SERVER['REQUEST_URI'],'episode1') !== false){ //DO STUFF }
Sorry, double post (hit submit twice) To make it worth while I suppose I'll post the code that worked for me: mysql_connect("localhost","",""); mysql_select_db("inbetweeners"); if(strpos($_SERVER['REQUEST_URI'],'series1/episode2') == true){ $url = 'series1/episode2'; } elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode3') == true) { $url = 'series1/episode3'; } elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode4') == true) { $url = 'series1/episode4'; } elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode5') == true) { $url = 'series1/episode5'; } elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode6') == true) { $url = 'series1/episode6'; } Code (markup):
using strpos isn't very smart, as /foo/episode1/bar, /view/episode1/taz... will all return the same thing. it can also be used to harm your site (getting 1000's of pages indexed with duplicate content under your domain isn't much helpful for your ranks) if you want to remove the first part of the path, try $path = $_SERVER['REQUEST_URI']; $path = substr($path, strpos($path, '/', 1) + 1); Code (markup): a request to hxttp://www.foo.com/bar/taz/baz will get "taz/baz" in $path