Okay.. so basically, say we have a link:
$url = "http://www.site.com/index.php?sub=Mawson&state=QLD&cat=4&page=2&sort=z";
Basically, I need to create a function, which replaces each thing in the URL, for example:
<a href="<?=$url;?>?sort=a">Sort by A-Z</a>
<a href="<?=$url;?>?sort=z">Sort by Z-A</a>
Or, for another example:
<a href="<?=$url;?>?cat=1">Category 1</a>
<a href="<?=$url;?>?cat=2">Category 2</a>
Or, another example:
<a href="<?=$url;?>?page=1">1</a>
<a href="<?=$url;?>?page=2">2</a>
<a href="<?=$url;?>?page=3">3</a>
<a href="<?=$url;?>?page=4">4</a>
So basically, we need a function which will replace the specific $_GET
from the URL so that we don't get a duplicate, such as: ?page=2&page=3
Having said that, it needs to be smart, so it knows if the beginning of the parameter is a ?
or an &
We also need it to be smart so that we can have the URL like so:
<a href="<?=$url;?>page=3">3</a> (without the ? - so it will detect automatically wether to use an `&` or a `?`
I don't mind making different variables for each preg_replace for the certain $_GET parameters, but I am looking for the best way to do this.
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…