How do I remove characters from a URL, and reload with them?
Moderator: General Moderators
-
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?
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 ??
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.
All the best from the United Kingdom.
Re: How do I remove characters from a URL, and reload with t
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
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.
All the best from the United Kingdom.
Re: How do I remove characters from a URL, and reload with t
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
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
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.
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
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.
All the best from the United Kingdom.
Re: How do I remove characters from a URL, and reload with t
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
Would you plz explain it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I remove characters from a URL, and reload with t
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
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.
All the best from the United Kingdom.
Re: How do I remove characters from a URL, and reload with t
First, I didn't realize that you're working with the entire URL and haven't pulled out just the query string yet. So
As for efficiency, what do you think?
Code: Select all
$newurl = strtok($_POST['url'], '?');
$qs = strtok('');
if ($qs) {
parse_str($qs, $qsarray);
unset($qsarray['rq']);
$newurl .= '?' . http_build_query($qsarray);
}-
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
No idea why, but:
Does not work. And keeps the full url with
[text]&rq=samp[/text]
At the end.
Mine works tho.
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);
}[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.
All the best from the United Kingdom.
Re: How do I remove characters from a URL, and reload with t
With
I get
What is the value of $_POST['url']?
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;Code: Select all
http://forums.devnetwork.net/viewtopic.php?f=1&t=137335