473,753 Members | 7,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Customizing unserialize output.

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
[totalResultsAva ilable] =20800000
[totalResultsRet urned] =20
[firstResultPosi tion] =1
[moreSearch] =>
/WebSearchServic e/V1/webSearch?query =bangalore&appi d=Jahangir&regi on=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.explocity.c om/bangalore.asp
[ClickUrl] =>
http://uk.wrs.yahoo.co m/_ylt=A0Je5ar3NI 5FgPAAtkDdmMwF; _ylu=X3oDMTB2cX VjNTM5BGNvbG8Dd wRsA1dTMQRwb3MD MQRzZWMDc3IEdnR pZAM-/SIG=11q4llbq3/EXP=1167033975/**http%3a//www.explocity.c om/bangalore.asp
[DisplayUrl] =>
www.explocity.c om/bangalore.asp
[ModificationDat e] =1166860800
[MimeType] =text/html
[Cache] =Array
(
[url] =>
http://uk.wrs.yahoo.co m/_ylt=A0Je5ar3NI 5FgPAAuEDdmMwF; _ylu=X3oDMTBwOH A5a2tvBGNvbG8Dd wRwb3MDMQRzZWMD c3IEdnRpZAM-/SIG=16i81upi9/EXP=1167033975/**http%3a//66.218.69.11/search/cache%3fei=UTF-8%26appid=Jahan gir%26query=ban galore%26output =php%26results= 20%26u=www.expl ocity.com/bangalore.asp%2 6w=bangalore%26 d=B-hzWEVuOAIw%26ic p=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.discoverban galore.com/
[ClickUrl] =>
http://uk.wrs.yahoo.co m/_ylt=A0Je5ar3NI 5FgPAAukDdmMwF; _ylu=X3oDMTB2Zj Q4dDExBGNvbG8Dd wRsA1dTMQRwb3MD MgRzZWMDc3IEdnR pZAM-/SIG=11ldrk2pv/EXP=1167033975/**http%3a//www.discoverban galore.com/
[DisplayUrl] =www.discoverba ngalore.com/
[ModificationDat e] =1166860800
[MimeType] =text/html
[Cache] =Array
(
[url] =>
http://uk.wrs.yahoo.co m/_ylt=A0Je5ar3NI 5FgPAAvEDdmMwF; _ylu=X3oDMTBwZG 5hOWwzBGNvbG8Dd wRwb3MDMgRzZWMD c3IEdnRpZAM-/SIG=16d2b45uj/EXP=1167033975/**http%3a//66.218.69.11/search/cache%3fei=UTF-8%26appid=Jahan gir%26query=ban galore%26output =php%26results= 20%26u=www.disc overbangalore.c om/%26w=bangalore% 26d=BCcdDEVuN_8 b%26icp=1%26.in tl=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($displa y as $disp)
{
$output['title']=$disp;
echo "$output";
foreach($disp as $disp2)
{
$newobj=$disp2;
echo "<br>$newob j";
foreach($disp2 as $key=>$value)
{
//$newobj2=$value ; // displays [moresearch]
$newobj3=$value[2];
echo "<br>$newob j3";
}}}
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

Dec 24 '06 #1
2 1749
Hi,

You can use the script below to do this. Pass the serialized data to
the method printResults:

--- Begin Text ---

<?

class YahooResultsVie w
{
function printResults($r awResponse)
{
$response = unserialize($ra wResponse);

$startHtml = $this->htmlEncode(
$response['ResultSet']['firstResultPos ition']);
echo "<ol start=\"$startH tml\">";

foreach ($response['ResultSet']['Result'] as $result) {
$clickUrlHtml = $this->htmlEncode($re sult['ClickUrl']);
$titleHtml = $this->htmlEncode($re sult['Title']);
$summaryHtml = $this->htmlEncode($re sult['Summary']);
$displayUrlHtml = $this->htmlEncode(
rtrim($result['DisplayUrl'], '/'));
$sizeHtml = $this->htmlEncode(
$this->formatMemorySi ze($result['Cache']['Size']));
$cacheUrlHtml = $this->htmlEncode($re sult['Cache']['Url']);

echo <<<end
<li><div><a class=title href="$clickUrl Html">$titleHtm l</a>
</div>
<div class=summary>$ summaryHtml</div>
<span class=url>$disp layUrlHtml</span-
<span class=size>$siz eHtml</span-
<a class=cache href="$cacheUrl Html">Cached</a>
end;
}

echo '</ol>';
}

// Convert bytes to the best unit of measurement

function formatMemorySiz e($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($s tr, 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 YahooResultsVie w;
$view->printResults(f ile_get_content s('response.txt '));

?>

</div>
</body>
</html>

--- End Text ---

thecoolone 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($displa y as $disp)
{
$output['title']=$disp;
echo "$output";
foreach($disp as $disp2)
{
$newobj=$disp2;
echo "<br>$newob j";
foreach($disp2 as $key=>$value)
{
//$newobj2=$value ; // displays [moresearch]
$newobj3=$value[2];
echo "<br>$newob j3";
}}}
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
Dec 25 '06 #2

petersprc wrote:
Hi,

You can use the script below to do this. Pass the serialized data to
the method printResults:

--- Begin Text ---

<?

class YahooResultsVie w
{
function printResults($r awResponse)
{
$response = unserialize($ra wResponse);

$startHtml = $this->htmlEncode(
$response['ResultSet']['firstResultPos ition']);
echo "<ol start=\"$startH tml\">";

foreach ($response['ResultSet']['Result'] as $result) {
$clickUrlHtml = $this->htmlEncode($re sult['ClickUrl']);
$titleHtml = $this->htmlEncode($re sult['Title']);
$summaryHtml = $this->htmlEncode($re sult['Summary']);
$displayUrlHtml = $this->htmlEncode(
rtrim($result['DisplayUrl'], '/'));
$sizeHtml = $this->htmlEncode(
$this->formatMemorySi ze($result['Cache']['Size']));
$cacheUrlHtml = $this->htmlEncode($re sult['Cache']['Url']);

echo <<<end
<li><div><a class=title href="$clickUrl Html">$titleHtm l</a>
</div>
<div class=summary>$ summaryHtml</div>
<span class=url>$disp layUrlHtml</span-
<span class=size>$siz eHtml</span-
<a class=cache href="$cacheUrl Html">Cached</a>
end;
}

echo '</ol>';
}

// Convert bytes to the best unit of measurement

function formatMemorySiz e($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($s tr, 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 YahooResultsVie w;
$view->printResults(f ile_get_content s('response.txt '));

?>

</div>
</body>
</html>
thanx peter.
But can u clarify why do u have the "formatMemorySi ze" function ??

Dec 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8129
by: Mark | last post by:
Greetings: So, I've got a real problem in that PHP doesn't seem to be able to unserialize() an array that has an element containing a newline. Of course, I've used htmlentities() to protect the serialized data. Here's a simple example that demonstrates the problem: ----- <?php
1
2207
by: Sebastian Aguilera | last post by:
Hi everyone. I have some troubles using the unserialize() function. I have serialized an array and that works perfect: echo '<input type="hidden" name="vardekedja" value="'.serialize($_POST ).'">'; Gives the output: a:3:{i:0;s:7:
2
2800
by: Andrew | last post by:
Some have suggested that using serialize() and unserialize is faster than reading/writing an array to disk as a simple text file using $array = file('numbers.txt'); Can anyone justify this? It seems to me that there is some overhead associated with calling unserialize().
7
5145
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For example, given: $data = serialize(array('4.50','0.00','0.00'));
1
1318
by: kjcox | last post by:
I've been using DocBook and XML for quite some time now. It works great for making simple HTML pages, http://gnuware.com/icecast/. I normally use it on RHEL and do not customize anything other than use the "docbook2html" command. It compiles my book and all chapters no problem. However, lately I have been needing to add custom fonts, unique colors to text and headers, and new items not normally covered under the...
5
3767
by: Mike | last post by:
I finally got serialize to work but now I cannot get unserialize to work. Here is my code. $settings = serialize($settings_array); echo "<INPUT TYPE='Hidden' NAME='settings' VALUE='".$settings."'>"; $settings = $_REQUEST; echo "<P>us=".$settings; $settings_array = Array();
20
1914
by: Ilias Lazaridis | last post by:
IDLE has an output format like this: <type 'object'> <type 'type'> <type 'type'> How can I customize it to become like that: <type 'object'> <type 'type'>
11
2350
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of policies, I want to customize the structure of a class. The idea is to investigate the use of several data structures. One option would be the use of the boost dynamic bitset. Another would be the use of the std::vector. I obtained some code that...
7
3559
by: powellgg | last post by:
I've just taken over a PHP website and am converting it to ASP.NET (don't shoot!). I'm not a PHP guy so I'm doing a lot of searching for things that I aren't obvious, and I'm hoping I'll be able to get help from here for things I can't find on the Internet. Here's the first... Here's the PHP code: setcookie("site_search_keyword","", time() - 3600); $site_search_keyword = unserialize(base64_decode($site_search_keyword));
0
9072
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9451
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9333
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4771
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3395
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.