Page 1 of 1

How do I remove characters from a URL, and reload with them?

Posted: Thu Jan 31, 2013 11:30 am
by simonmlewis
We are passing thru a variable that stores the URL of a page.
It gets to the product page, and an extra part of that URL "....&rq=yes", it what triggers something.
But then I want to remove those specific characters from $url.

How would I go about this?

Do I capture the $url contents, and find the last character, and delete, for example -7 (the last 7 characters), and reassign back to $url ??

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 11:36 am
by Benjamin
Without seeing an example, I'm thinking you can just send a header redirect to the new address, even if it's the same page.

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 11:39 am
by simonmlewis
How does it know the address, if the address as a variable on the end that will take them back to this page again? That's why I need to remove the end bit.

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 11:51 am
by Benjamin
If it's a variable you can just grab it or use parse_url() to parse it out.

http://www.php.net/manual/en/function.parse-url.php

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 12:00 pm
by simonmlewis
I want to remove it from the url, and then use that remaining url.

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 1:02 pm
by simonmlewis
I've sussed it. I knew there had to be a way to remove text.

Code: Select all

$url = $_POST['url'];

if (strpos($url,'rq=samp') !== false) {
$urlremove = array("&rq=samp");
$newurl = str_replace($urlremove, "", "$url");
}

if (strpos($url,'rq=broch') !== false) {
$urlremove = array("&rq=broch");
$newurl = str_replace($urlremove, "", "$url");
}

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 3:44 pm
by requinix

Code: Select all

parse_str($_POST['url'], $url);
unset($url['rq']);
$newurl = http_build_query($url);

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 3:46 pm
by simonmlewis
Would you plz explain it?

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 4:29 pm
by requinix
The parse_url() that Ben linked you to will turn a key=value string (like a query string) into an array (like $_GET). Then unset() to remove the key you don't want, and http_build_query to reassemble the array into a string.

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 4:32 pm
by simonmlewis
Is that a more effective way, than the method I figured out?

Re: How do I remove characters from a URL, and reload with t

Posted: Thu Jan 31, 2013 5:06 pm
by requinix
First, I didn't realize that you're working with the entire URL and haven't pulled out just the query string yet. So

Code: Select all

$newurl = strtok($_POST['url'], '?');
$qs = strtok('');

if ($qs) {
    parse_str($qs, $qsarray);
    unset($qsarray['rq']);
    $newurl .= '?' . http_build_query($qsarray);
}
As for efficiency, what do you think?

Re: How do I remove characters from a URL, and reload with t

Posted: Fri Feb 01, 2013 4:51 am
by simonmlewis
No idea why, but:

Code: Select all

$url = $_POST['url'];

$newurl = strtok($_POST['url'], '?');
$qs = strtok('');

if ($qs) {
    parse_str($qs, $qsarray);
    unset($qsarray['rq']);
    $newurl .= '?' . http_build_query($qsarray);
}
Does not work. And keeps the full url with
[text]&rq=samp[/text]
At the end.

Mine works tho.

Re: How do I remove characters from a URL, and reload with t

Posted: Fri Feb 01, 2013 12:14 pm
by requinix
With

Code: Select all

$_POST['url'] = 'http://forums.devnetwork.net/viewtopic.php?f=1&t=137335&rq=123';

$newurl = strtok($_POST['url'], '?');
$qs = strtok('');

if ($qs) {
    parse_str($qs, $qsarray);
    unset($qsarray['rq']);
    $newurl .= '?' . http_build_query($qsarray);
}
echo $newurl;
I get

Code: Select all

http://forums.devnetwork.net/viewtopic.php?f=1&t=137335
What is the value of $_POST['url']?