TheMagician Posted April 15, 2009 Share Posted April 15, 2009 <?php function returnLink($url, $title, $alt=null) { // Return either link or normal text // If $alt is not null, append text after the link if(!is_null($alt)) { if(!strpos($_SERVER['REQUEST_URI'], $url)) { echo sprintf('<a href="%s">%s</a> %s', $url, $title, $alt); } else { echo sprintf('<p class="selected_link">%s</p> %s', $title, $alt); } } else { if(!strpos($_SERVER['REQUEST_URI'], $url)) { echo sprintf('<a href="%s">%s</a>', $url, $title); } else { echo sprintf('<p class="selected_link">%s</p>', $title); } } } ?> This function works pretty well for situations like the following: <?php returnLink("?foo=1"); returnLink("?foo=5&bar=6"); ?> But if you have something like this: <?php returnLink("?foo=1"); returnLink("?foo=10"); ?> Then both of the links are outputted as normal text, which is what I don't want. How should I modify my function to also take this into consideration? Thanks. edit: Added PHP tags. Quote Link to comment https://forums.phpfreaks.com/topic/154193-solved-comparing-url-to-a-variable/ Share on other sites More sharing options...
Zhadus Posted April 15, 2009 Share Posted April 15, 2009 You're checking to see if the current URI contains the 'url' that you're passing to it. If the URI is 'blah.php?foo=10' then it contains BOTH '?foo=1' & '?foo=10'. I'd recommend using regular expressions to separate the variables etc. and check them using a loop. Otherwise you could accomplish the same thing with some messy explodes and separate the .php page from the variables and check those. Quote Link to comment https://forums.phpfreaks.com/topic/154193-solved-comparing-url-to-a-variable/#findComment-810656 Share on other sites More sharing options...
premiso Posted April 15, 2009 Share Posted April 15, 2009 Or use PHP's built in function parse_str that one or a comparable one (seen in the notes of that page). Quote Link to comment https://forums.phpfreaks.com/topic/154193-solved-comparing-url-to-a-variable/#findComment-810662 Share on other sites More sharing options...
TheMagician Posted April 15, 2009 Author Share Posted April 15, 2009 I think I fixed it, problem solved unless someone can find a problem in it. <?php function returnLink($url, $title, $alt=null) { // Return either link or normal text // If $alt is not null, append text after the link $real_url = '?' . $_SERVER['QUERY_STRING']; if(!is_null($alt)) { if(($real_url == $url) || strpos($real_url, $url . '&') !== false) { echo sprintf('<p class="selected_link">%s</p> %s', $title, $alt); } else { echo sprintf('<a href="%s">%s</a> %s', $url, $title, $alt); } } else { if(($real_url == $url) || strpos($real_url, $url . '&') !== false) { echo sprintf('<p class="selected_link">%s</p>', $title); } else { echo sprintf('<a href="%s">%s</a>', $url, $title); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154193-solved-comparing-url-to-a-variable/#findComment-810712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.