Home > Archive > PHP Programming > December 2006 > Customizing unserialize output.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Customizing unserialize output.
|
|
| thecoolone 2006-12-24, 3:59 am |
| I am trying to implement the yahoo search api using php.
I prefer to use PHP serialized to get the result of the search
displayed.
Right now i get the output like:
Array
(
[ResultSet] => Array
(
[type] => web
[totalResultsAvailable] => 20800000
[totalResultsReturned] => 20
[firstResultPosition] => 1
[moreSearch] =>
/WebSearchService/V1/webSearch?query=bangalore&appid=Jahangir®ion=us
[Result] => Array
(
[0] => Array
(
[Title] => Explocity.com Bangalore. The
city as it happens
[Summary] => Offers an event schedule,
places to see, shopping, dining, and other information about Bangalore.
=>
[url]http://www.exploc...m/bangalore.asp
[ClickUrl] =>
http://uk.wrs.yahoo.com/ _ylt=A0Je5...m/bangalore.asp
[DisplayUrl] =>
www.explocity.com/bangalore.asp
[ModificationDate] => 1166860800
[MimeType] => text/html
[Cache] => Array
(
=>
[url]http://uk.wrs.yah...esults=20%26u=w
ww.explocity.com/bangalore.asp%26w=bangalore%26d=B-hzWEVuOAIw%26icp=1%26.intl=us
[Size] => 28482
)
)
[1] => Array
(
[Title] => Bangalore
[Summary] => Tourist guide around the city
of Bangalore including historical background, weather, dining,
entertainment, and accommodations.
=> [url]http://www.discoverbangalore.com/
[ClickUrl] =>
http://uk.wrs.yahoo.com/ _ylt=A0Je5...rbangalore.com/
[DisplayUrl] => www.discoverbangalore.com/
[ModificationDate] => 1166860800
[MimeType] => text/html
[Cache] => Array
(
=>
[url]http://uk.wrs.yah...esults=20%26u=w
ww.discoverbangalore.com/ %26w=bangalore%26d=BCcdDEVuN_8b%26icp=1%
26.intl=us
[Size] => 25882
)
I am trying to format this using 2 foreach loop. One for Array second
for ResultSet and Third for individual Result.
Example of what im trying:
foreach($display as $disp)
{
$output['title']=$disp;
echo "$output";
foreach($disp as $disp2)
{
$newobj=$disp2;
echo "<br>$newobj";
foreach($disp2 as $key=>$value)
{
//$newobj2=$value; // displays [moresearch]
$newobj3=$value[2];
echo "<br>$newobj3";
}}}
But i am having a hard time getting the output that i want.
Can someone help me format the output got from unserialize() in the
same way as yahoo displays its result on its main page.
thanks in advance
| |
| petersprc 2006-12-25, 3:59 am |
| Hi,
You can use the script below to do this. Pass the serialized data to
the method printResults:
--- Begin Text ---
<?
class YahooResultsView
{
function printResults($rawResponse)
{
$response = unserialize($rawResponse);
$startHtml = $this->htmlEncode(
$response['ResultSet']['firstResultPosit
ion']);
echo "<ol start=\"$startHtml\">";
foreach ($response['ResultSet']['Result'] as $result) {
$clickUrlHtml = $this->htmlEncode($result['ClickUrl']);
$titleHtml = $this->htmlEncode($result['Title']);
$summaryHtml = $this->htmlEncode($result['Summary']);
$displayUrlHtml = $this->htmlEncode(
rtrim($result['DisplayUrl'], '/'));
$sizeHtml = $this->htmlEncode(
$this-> formatMemorySize($result['Cache']['Size'
]));
$cacheUrlHtml = $this->htmlEncode($result['Cache']['Url']);
echo <<<end
<li><div><a class=title href="$clickUrlHtml">$titleHtml</a>
</div>
<div class=summary>$summaryHtml</div>
<span class=url>$displayUrlHtml</span> -
<span class=size>$sizeHtml</span> -
<a class=cache href="$cacheUrlHtml">Cached</a>
end;
}
echo '</ol>';
}
// Convert bytes to the best unit of measurement
function formatMemorySize($bytes)
{
if ($bytes < 0x3E8) {
return $bytes . 'b'; // bytes
} elseif ($bytes < 0xF4240) {
return round($bytes / 0x3E8) . 'k'; // kilobytes
} elseif ($bytes < 0x3B9ACA00) {
return round($bytes / 0xF4240) . 'M'; // megabytes
} elseif ($bytes < 0x3B9ACA00 * 0x3E8) {
return round($bytes / 0x3B9ACA00) . 'G'; // gigabytes
}
return round($bytes / 0x3B9ACA00 * 0x3E8) . 'T'; // terabytes
}
function htmlEncode($str)
{
return htmlentities($str, ENT_QUOTES);
}
}
?>
<html>
<head>
<style>
body {font: 83%/1.2em arial,helvetica,clean,sans-serif;}
#results {width: 600px;}
#results a {color: #0000de;}
#results a:visited {color: #639}
#results a:active {color: #f00;}
#results li {margin: 0 0 17px 21px;}
#results .title {font-size: 120%;}
#results .summary {color: #000;}
#results .url {color: #008000;}
#results .size {color: #8284cc;}
#results .cache {color: #8284cc;}
</style>
</head>
<body>
<div id=results>
<?
$view = new YahooResultsView;
$view-> printResults(file_get_contents('response
.txt'));
?>
</div>
</body>
</html>
--- End Text ---
the one wrote:
> I am trying to implement the yahoo search api using php.
> I prefer to use PHP serialized to get the result of the search
> displayed.
> I am trying to format this using 2 foreach loop. One for Array second
> for ResultSet and Third for individual Result.
> Example of what im trying:
> foreach($display as $disp)
> {
> $output['title']=$disp;
> echo "$output";
> foreach($disp as $disp2)
> {
> $newobj=$disp2;
> echo "<br>$newobj";
> foreach($disp2 as $key=>$value)
> {
> //$newobj2=$value; // displays [moresearch]
> $newobj3=$value[2];
> echo "<br>$newobj3";
> }}}
> But i am having a hard time getting the output that i want.
>
> Can someone help me format the output got from unserialize() in the
> same way as yahoo displays its result on its main page.
>
> thanks in advance
| |
| thecoolone 2006-12-25, 7:00 pm |
|
petersprc wrote:
> Hi,
>
> You can use the script below to do this. Pass the serialized data to
> the method printResults:
>
> --- Begin Text ---
>
> <?
>
> class YahooResultsView
> {
> function printResults($rawResponse)
> {
> $response = unserialize($rawResponse);
>
> $startHtml = $this->htmlEncode(
> $response['ResultSet']['firstResultPosit
ion']);
> echo "<ol start=\"$startHtml\">";
>
> foreach ($response['ResultSet']['Result'] as $result) {
> $clickUrlHtml = $this->htmlEncode($result['ClickUrl']);
> $titleHtml = $this->htmlEncode($result['Title']);
> $summaryHtml = $this->htmlEncode($result['Summary']);
> $displayUrlHtml = $this->htmlEncode(
> rtrim($result['DisplayUrl'], '/'));
> $sizeHtml = $this->htmlEncode(
> $this-> formatMemorySize($result['Cache']['Size'
]));
> $cacheUrlHtml = $this->htmlEncode($result['Cache']['Url']);
>
> echo <<<end
> <li><div><a class=title href="$clickUrlHtml">$titleHtml</a>
> </div>
> <div class=summary>$summaryHtml</div>
> <span class=url>$displayUrlHtml</span> -
> <span class=size>$sizeHtml</span> -
> <a class=cache href="$cacheUrlHtml">Cached</a>
> end;
> }
>
> echo '</ol>';
> }
>
> // Convert bytes to the best unit of measurement
>
> function formatMemorySize($bytes)
> {
> if ($bytes < 0x3E8) {
> return $bytes . 'b'; // bytes
> } elseif ($bytes < 0xF4240) {
> return round($bytes / 0x3E8) . 'k'; // kilobytes
> } elseif ($bytes < 0x3B9ACA00) {
> return round($bytes / 0xF4240) . 'M'; // megabytes
> } elseif ($bytes < 0x3B9ACA00 * 0x3E8) {
> return round($bytes / 0x3B9ACA00) . 'G'; // gigabytes
> }
> return round($bytes / 0x3B9ACA00 * 0x3E8) . 'T'; // terabytes
> }
>
> function htmlEncode($str)
> {
> return htmlentities($str, ENT_QUOTES);
> }
> }
>
> ?>
>
> <html>
> <head>
> <style>
> body {font: 83%/1.2em arial,helvetica,clean,sans-serif;}
> #results {width: 600px;}
> #results a {color: #0000de;}
> #results a:visited {color: #639}
> #results a:active {color: #f00;}
> #results li {margin: 0 0 17px 21px;}
> #results .title {font-size: 120%;}
> #results .summary {color: #000;}
> #results .url {color: #008000;}
> #results .size {color: #8284cc;}
> #results .cache {color: #8284cc;}
> </style>
> </head>
> <body>
> <div id=results>
>
> <?
>
> $view = new YahooResultsView;
> $view-> printResults(file_get_contents('response
.txt'));
>
> ?>
>
> </div>
> </body>
> </html>
>
thanx peter.
But can u clarify why do u have the "formatMemorySize" function ??
|
|
|
|
|