Solved

array to table?

Posted on 2010-08-24
9
228 Views
Last Modified: 2012-05-10
Hi,
Please see the attached code.  I am struggle to print the array which currently prints to the web page like this:

Array ( [0] => http://www.adriennespizzabar.com/ [1] => http://www.johnsbrickovenpizza.com/ [2] => http://www.firstpizza.com/ [3] => http://www.johnspizzerianyc.com/ [4] => http://www.ottopizzeria.com/ [5] => http://www.eastvillagepizzeria.com/ [6] => http://www.joespizza.com/ ) Array ( [0] => Adrienne's Pizza Bar Restaurant [1] => John's Pizzeria of Bleecker Street [2] => Lombardi's Pizza [3] => John's Pizzeria [4] => Otto Restaurant Enoteca Pizza [5] => East Village Pizza and Kebabs [6] => Joe's Pizza )

But I want it to display in a table like this:

http://www.adriennespizzabar.com/     |  Adrienne's Pizza
http://www.johnsbrickovenpizza.com/  |  John's Pizzeria of Bleecker Street
http://www.firstpizza.com/                  |  Lombardi's Pizza

Can someone please help me with the code to achieve this?

Thanks very much
<?php

$page = file_get_contents('http://www.google.com/search?q=new+york+pizza');
 
preg_match_all(
    '@<h4[^>]*class=r[^>]*>\s*<a\s*href="(.*)".*class=l.*title="(.*)".*>@siU',
    $page,
    $matches);
 
print_r($matches[1]); // urls
print_r($matches[2]); // titles

?>

Open in new window

0
Comment
Question by:chrisj1963
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 33517488
replace
print_r($matches[1]); // urls
print_r($matches[2]); // titles

With

foreach ($matches as $val) {
 echo  $val[1] . "|" . $val[2]  "\n";
}
0
 
LVL 10

Expert Comment

by:honestman31
ID: 33517496
replace
print_r($matches[1]); // urls
print_r($matches[2]); // titles

With

foreach ($matches as $val) {
 echo  $val[1] . "     |       " . $val[2]  "\n";
}
0
 
LVL 2

Accepted Solution

by:
dfendig earned 500 total points
ID: 33517505
Don't use print_r().

change it to this:
for($counter = 0; $counter < count($matches[1]);$counter++)
{
    echo $matches[1][$counter];
    echo '&nbsp $nbsp | &nbsp &nbsp';
    echo $matches[2][$counter];
    echo '<br />';
}

of course you could modify that code to format the data in any way, including echoing html tags to create a html table.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:chrisj1963
ID: 33517506
Thanks for the response, but it gives me a blank page result... any other thoughts on how to make it work?
thanks!
<?php

$page = file_get_contents('http://www.google.com/search?q=new+york+pizza');
 
preg_match_all(
    '@<h4[^>]*class=r[^>]*>\s*<a\s*href="(.*)".*class=l.*title="(.*)".*>@siU',
    $page,
    $matches);
 
//print_r($matches[1]); // urls
//print_r($matches[2]); // titles

foreach ($matches as $val) {
 echo  $val[1] . "     |       " . $val[2]  "\n";
}
?>

Open in new window

0
 

Author Closing Comment

by:chrisj1963
ID: 33517522
that was it. thanks very much!
0
 
LVL 10

Expert Comment

by:honestman31
ID: 33517545
sorry there is a missing dot in my code ( after 2]  )
I know it is over but please try it

foreach ($matches as $val) {
 echo  $val[1] . "     |       " . $val[2]  . "\n";
}
0
 

Author Comment

by:chrisj1963
ID: 33517567
thanks for that. The code looked much leaner, but it produces one long link:
http://www.johnsbrickovenpizza.com/ | http://www.firstpizza.com/ John&#39;s Pizzeria of Bleecker Street | Lombardi&#39;s Pizza  
0
 
LVL 2

Expert Comment

by:dfendig
ID: 33517770
"\n" doesn't display in a browser, you have to put a line break <br />
Just use honestman's cleaner code and replace the \n with the line break
0
 

Author Comment

by:chrisj1963
ID: 33517806
duh.... I've got it now.  thanks very much for your follow-up....:)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question