Can you strip out text from a URL?

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:

Can you strip out text from a URL?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
rolanddev
Forum Newbie
Posts: 9
Joined: Mon Jul 05, 2010 12:28 pm
Location: Bucharest

Re: Can you strip out text from a URL?

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

Re: Can you strip out text from a URL?

Post 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".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Can you strip out text from a URL?

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

Re: Can you strip out text from a URL?

Post 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 &.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
rolanddev
Forum Newbie
Posts: 9
Joined: Mon Jul 05, 2010 12:28 pm
Location: Bucharest

Re: Can you strip out text from a URL?

Post 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!
Last edited by rolanddev on Mon Jul 05, 2010 1:27 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Can you strip out text from a URL?

Post by John Cartwright »

I edited my original reply with a solution.
Post Reply