clean and rearange query string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

clean and rearange query string

Post by louie35 »

I need some help with this.

First - rearange the query string

eg. page.php?mid=123&pid=123

to

eg. page.php?pid=123&mid=123

:::::::::::

Second - I want to clean the query string from duplicate values

eg. page.php?pid=123&mid=123&pid=144

the bold part is the duplicate no matter what value it has i want to keep the first one

eg. eg. page.php?pid=123&mid=123
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Couple of questions....

1) Where are these links being created ?
2) Why do they need to be tidied ?

It would make a lot of sense to correct the creation of the links/passthoughs rather than correct them after they are sent. You could do things with $_SERVER['REDIRECT_URL'] but I would try to avoid it if possible. Make the original links correct first.

If you have page.php?pid=123&mid=123&pid=144 for some reason and you have pid=123 as the main thing, you may wish to save the value in a session but without having any idea on the workflow/code it is hard to be more precise.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

I am triyng to use mode-rewrite and shorten the links form

page.php?pid=123&mid=123&pname=product_name&mname=cat_name

to

page/123/123/product_name/cat_name

the reason i need to rearange them.
I have a function to modifies the links created as the website wasn't design with mode-rewrite in mind from start but all links are parsed by this funtion

Code: Select all

function link_url($page){
global $km,$kc,$kb,$ks,$p_id;
$url_link = $page."?";

		$url_link = str_replace(",","_",$url_link);
		$url_link = str_replace("-","_",$url_link);
		$url_link = str_replace(">","_",$url_link);
		$url_link = str_replace("/","_",$url_link);
		$url_link = str_replace("(","_",$url_link);
		$url_link = str_replace(")","_",$url_link);
		$url_link = str_replace(" ","_",$url_link);
		$url_link = str_replace("~","_",$url_link);
		$url_link = str_replace('__','_',$url_link);
		$url_link = str_replace('__','_',$url_link);
		$url_link = str_replace('__','_',$url_link);
		$url_link = str_replace('__','_',$url_link);
		$url_link = str_replace("??","?",$url_link);
		$url_link = rtrim($url_link,"?");//change the / to ?
	
	return $url_link;
}
i could easily rearange them inside the function using if..then statements but how about removing duplicates just in case.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Eek... Mod rewrite... 8O. sorry only know the basics for that...

I would still try to change the function to get the URL correct. You may want to use preg_match_all to get all the options, save them in array and do an array_unique or something similar.

If this is legacy code you have to ask yourself, how much time would it take to get the code working properly rather than "hack it" to get it working with mod rewrite (if it can be done).
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

mod_rewrite is definitely a good solution for you.

You really don't have to change much in terms of code. All you need to do is make sure your links are updated and presto, the rest is done for you if you apply the right rewrite rule(s)

We have a forum that covers that topic: viewforum.php?f=31

You'll find many good examples there. Plus there is always Google if you're looking for tutorials.

If you get stuck, feel free to post a question. I know there is many people highly experienced with mod_rewrite in this community (me being one) and we'll be happy to help you solve the problem.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

thanks.

I am working at the moment on re-aranging the querystring after which I'll be loking to get the right mod-rewrite in place using .htaccess

The website has too many pages to edit the links manually but as I said all links are parsed by the function above, which I am working on adding more features to create them the right way.
Hving problems with duplicates but I'll get arround that (hope)
Post Reply