Return to Snippet

Revision: 44737
at April 17, 2011 01:12 by prwhitehead


Initial Code
/**
* Usage: 
* <?php $url = 'http://www.yourwebsite.com/?page=1&showposts=10'; ?>
* <a href="<?php echo safe_urls($url, 'page', '1'); ?>" title="Page 1">Page 1</a>
* <a href="<?php echo safe_urls($url, 'page', '2'); ?>" title="Page 2">Page 2</a>
* <a href="<?php echo safe_urls($url, 'page', '3'); ?>" title="Page 3">Page 3</a>
*
* <a href="<?php echo safe_urls($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'page', '3'); ?>" title="Page 3">Page 3</a>
*/

function safe_urls($url = '', $var = 'page', $value = '1'){

	if(strpos($url, '?'))
	{
		if(strpos($url, $var))
		{
			$i = explode('?', $url);
			foreach($i as $arg)
			{
				$k = explode('&', $arg);
				foreach($k as $t)
				{
					$s = explode('=', $t);
					if ($s[0] == $var) $match = $s[0] . '=' . $s[1];
				}
			}
			$url = str_replace($match, $var . '=' . $value, $url);
		}
		else
		{
			$url .= '&' . $var . '=' . $value;		
		}
	}
	else
	{
		$url .= '?' . $var . '=' . $value;	
	}
	return $url;
}

Initial URL

                                

Initial Description
Make sure your URL $_GET variables don't get repeated / duplicated when you are creating links / URLs dynamically.

Initial Title
Safe Link Arguments

Initial Tags
php, links

Initial Language
PHP