Jump to content

Recommended Posts

<?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.

Link to comment
https://forums.phpfreaks.com/topic/154193-solved-comparing-url-to-a-variable/
Share on other sites

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.

 

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);
	}
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.