Hi,
I was wondering, how do I parse a url. For instance, the result from the xml may be 'www.uncoverthenet.com/search/search.php?q=cars', but I want it to only show 'www.uncoverthenet.com'. How would I do that? I can't seem to get php's parse_url function to work here, but I'm probably doing something wrong.
Thanks,
Shawn
Hi David,
Sorry, I should have explained better. I can get the parse_url function to work, just not with the resultset from magic parser. For instance my code is:
<?php
function myRecordHandler($item)
{
print "<p><a href='".urldecode($item["URL"])."'>".utf8_decode($item["NAME"])."</a>";
print "<font size=-1>";
print "<BR>".utf8_decode($item["DESCRIPTION"])."<br><font class=a>".$item["HTTPLINK"]."</font></font></p>";
}
$url = "http://sp.uncoverthenet.com/feed/xml.aspx?affiliate=68981&qu=$q123&pn=1&r=10&filter=no&ip_address=$ip&st=typein&rid=uncoverthenet";
MagicParser_parse($url,"myRecordHandler","xml|RESULTS/SITE/");
?>
So how would I change ".$item["HTTPLINK"]." to be just the sitehost?
Thanks,
Shawn
Hi Shawn,
url_decode returns an associative array containing the parts of the URL. To access the host name, you then need to access that array with the key "host". Have a go with this code (based on yours above):
<?php
function myRecordHandler($item)
{
$parts = urldecode($item["URL"]);
$url = "http://".$parts["host"];
print "<p><a href='".$url."'>".utf8_decode($item["NAME"])."</a>";
print "<font size=-1>";
print "<BR>".utf8_decode($item["DESCRIPTION"])."<br><font class=a>".$item["HTTPLINK"]."</font></font></p>";
}
$url = "http://sp.uncoverthenet.com/feed/xml.aspx?affiliate=68981&qu=$q123&pn=1&r=10&filter=no&ip_address=$ip&st=typein&rid=uncoverthenet";
MagicParser_parse($url,"myRecordHandler","xml|RESULTS/SITE/");
?>
Note how i've added the http:// because after using url_explode the host component is only the domain name...
Hope this helps!
Cheers,
David.
OK, now I see. The code makes sense, but I can't get it to work. I'm trying to change the HTTPLINK not the URL. So I modified the suggested code to:
<?php
function myRecordHandler($item)
{
$parts = $item["HTTPLINK"];
$domain = $parts["host"];
print "<font class=a>".$domain."</font>";}
?>
But it only prints out "h". Am I doing something wrong?
Thanks again,
Shawn
Ok, I figured it out by basically doing this:
$myDomain = parse_url($item["HTTPLINK"]);
and then using ".$myDomain["host"]." instead of $item["HTTPLINK"]. So thank you for the help.
And I hate to be a pest :-) , but is there a way to break the results in two? For instance, in normal php I would use something like:
<?php
$chunked_arrays1 = array_chunk($results, 3);
$p1 = $chunked_arrays1[0];
$p2 = $chunked_arrays1[1];
?>
And then do something like
<?php
if ($p1){
blahaha
}
?>
Is there a way to do that using this parser?
Thanks again,
Shawn
Hi Shawn,
I'm afraid i'm not really sure what you mean there. Do you want to split up the result of the parse_url(0 function?
Cheers,
David.
No, sorry I mean split up the results returned from the xml (nothing to do with parse_url). For instance, if there were 10 results returned. How could I split them up into two groups of 5, so that I could use the first 5 on the top of the page and the second 5 on the bottom of the page? Make sense?
Thanks,
Shawn
Hi Shawn,
Sorry - yes I understand. That's quite easy to do - here's the outline code based on your code above...
<?php
$page = $_GET["page"];
$counter = 0;
$itemsOnPage = 10;
// default to the first page
if (!$page) $page = 1;
function myRecordHandler($item)
{
global $page;
global $counter;
global $itemsOnPage;
$counter++;
// return false whilst parsing items on previous pages
if ($counter < (($page-1)*$itemsOnPage)) return false;
// display the item
/***********************/
/* YOUR CODE GOES HERE */
/***********************/
// return true if counter reaches current page * items on each page
return ($counter == ($page*$itemsOnPage));
}
// parse the feed
$url = "http://sp.uncoverthenet.com/feed/xml.aspx?affiliate=68981&qu=$q123&pn=1&r=10&filter=no&ip_address=$ip&st=typein&rid=uncoverthenet";
MagicParser_parse($url,"myRecordHandler","xml|RESULTS/SITE/");
// display link to next page
print "<p><a href='?page=".($page+1)."'>Next</a></p>";
?>
Simply replace "YOUR CODE GOES HERE" with the code you are currently using to display the item in myRecordHandler.
Hope this helps point you in the right direction...
Cheers,
David.
Thank you for the reply. But the best I can guess is that the code you posted above paginates the results?
I'm looking to separate them on the same page. So 10 results could be split up into 2 groups of results with page content in the middle. Is this possible?
So it would be
result1
result2
result3
result4
result5
page content
result6
result7
result8
result9
result10
Does that make sense? I'm sorry to be pest :)
Ok, I think I know how to phrase it now. I'm looking to use php's foreach function with this parser. How do I do that? :-)
Thanks,
Shawn
Hi Shawn,
Yes - I see what you mean. There are quite a few ways you could code this up, but this is what I think I would do:
1) Parse all $items into a global array via myRecordHandler()
2) Create a second function to display a $item
3) Setup a counter so that you know where you've got to in the array
4) Display n items
5) Create content
6) Display n more items
Here's some outline code to demonstrate what I mean:
<?php
$items = array();
$pointer = 0;
function myRecordHandler($item)
{
global $items;
// add this item to the global array
$items[] = $item;
}
// parse the items into the global array
$url = "[snipped]";
MagicParser_parse($url,"myRecordHandler","xml|RESULTS/SITE/");
// create a function to display a $item
function displayItem($item)
{
// your code to display an here, same as in myRecordHandler() before
}
// display first 5 items
for($i=0;$i<=5;$i++)
{
if (is_array($items[$i])) displayItem($items[$i]);
}
// create page content here
// display next 5 items
for($i=5;$i<=10;$i++)
{
if (is_array($items[$i])) displayItem($items[$i]);
}
// create page content here etc. etc.
?>
That's one way to do it. You would need to something more complex if you want to handle an unknown number of results and display content every n records; for example creating an array of page content; using foreach() to display the items and then displaying an item from the content array every 5 items....
Hope this helps!
Cheers,
David.
Hi Shawn,
I've just tested this and it looks like parse_url requires http:// to be infront of the URL. The following code works:
<?php
$url = "http://www.uncoverthenet.com/search/search.php?q=cars";
$parts = parse_url($url);
print $parts["host"];
?>
If the value you get from the feed does not have http:// at the beginning, simply prefix the string with "http://" before passing the value to parse_url(), and you should then get the host name out fine.
Cheers,
David.