/ Published in: PHP
Make sure your URL $_GET variables don't get repeated / duplicated when you are creating links / URLs dynamically.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * 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; }