Page 1 of 1
Can you strip out text from a URL?
Posted: Mon Jul 05, 2010 10:49 am
by simonmlewis
Ok this sounds like a toughie to me, but may be simple.
I am posting a category (see another comment of mine here) to a URL. But the user may click other links in the list, to post secondary and third category attempts to find what they want.
So you may have ......index.php?page=go&category=today&category=tomorrow&category=yesterday.
The previous ones are there because it is posting it as $url&category=$row->category.
$url is using the script to pick up the URL.
I know PHP picks up the last one, but is there a script where you can remove everything in the URL (starting from the right) all the way to to the last 'category'??? So all you have left is ....page=go.
Re: Can you strip out text from a URL?
Posted: Mon Jul 05, 2010 12:51 pm
by rolanddev
Code: Select all
<?php
//change this
$url="index.php?page=go&category=today&category=tomorrow&category=yesterday.";
$first = explode('?', $url);
$first = $first[1];
$first = explode('page=', $first);
$first = $first[1];
$first = explode('&', $first);
$first = $first[0];
//the page needed
$page= $first;
// total sintagme
$total = "page=".$page;
//echo all
echo "Page: <b>". $page."</b><br><br>";
echo "Total: <b>". $total."</b><br>";
?>
I wrote this code for you!
You can use the explode function, more times until you get what you want in your variable, verify the code to see what am i talking about...
I hope I helped you!
Re: Can you strip out text from a URL?
Posted: Mon Jul 05, 2010 12:59 pm
by simonmlewis
Hi - thanks for your work there.
Perhaps I should have also mentioned there will be other variables in there to keep.
ie.
index.php?page=home&id1-332&id2=442&category=today&category=tomorrow.......
The "id1, 2".... could be all the way up to 14.
It may be there this doesn't really matter, because if it accepts always the LAST "category", and once someone selects something from the list shown from the variable it wipes it back to just the id1, 2, 3, 4..... then it could be a case of "sod it - leave as is".
Re: Can you strip out text from a URL?
Posted: Mon Jul 05, 2010 1:15 pm
by John Cartwright
I don't want to offer a solution to this because it would merely be a bandaid. Why don't you fix your links to only include 1 instance of each variable.
However, for the sake of moving forward, heres a quick function that will do exactly what you want.
Code: Select all
function uniqueQueryString($url)
{
$query = '';
$urlparts = parse_url($url);
if (array_key_exists('query', $urlparts)) {
parse_str($urlparts['query'], $queryparts);
$query = '?'. http_build_query($queryparts);
}
return $urlparts['scheme'] .'://'. $urlparts['host'] . $urlparts['path'] . $query;
}
echo uniqueQueryString('http://google.com/index.php?page=home&id1-332&id2=442&category=today&category=tomorrow');
Re: Can you strip out text from a URL?
Posted: Mon Jul 05, 2010 1:18 pm
by simonmlewis
I appreciate that.
I haven't a clue how to do that, but it would be the best option.
When the page loads, it has category=whatever in it. But then you may want to select an alternative category.... and that has $url&category=$row->category in the link. so it's posting exactly what is up there.
So not sure how to strip out that last category and the &.
Re: Can you strip out text from a URL?
Posted: Mon Jul 05, 2010 1:25 pm
by rolanddev
Code: Select all
<?php
//change this
$url="index.php?id=23&page=go&category=today&category=tomorrow&category=yesterday.";
$first = explode('?', $url);
$first = $first[1];
$first = explode('page=', $first);
$first = $first[1];
$first = explode('&', $first);
$first = $first[0];
//the page needed
$page= $first;
// total sintagme
$total = "page=".$page;
//echo all
echo "Page: <b>". $page."</b><br><br>";
echo "Total: <b>". $total."</b><br>";
/* start id searching */
$id = explode('?', $url);
$id = $id[1];
$id = explode('id=', $id);
$id = $id[1];
$id = explode('&', $id);
$id = $id[0];
$id = "id=".$id;
//echo id
echo "<br><br><u><font color=red>STARTING ID LISTING</font></u> <BR><BR>id: <b>". $id."</b><br><br>";
echo "Total id: <b>". $total."</b><br>";
?>
I Updated so it extracts the Id as well! Hope I was helpful!
Re: Can you strip out text from a URL?
Posted: Mon Jul 05, 2010 1:33 pm
by John Cartwright
I edited my original reply with a solution.