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

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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 ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

I want to remove it from the url, and then use that remaining url.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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");
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Code: Select all

parse_str($_POST['url'], $url);
unset($url['rq']);
$newurl = http_build_query($url);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Would you plz explain it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Is that a more effective way, than the method I figured out?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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']?
Post Reply