Solved

array to table?

Posted on 2010-08-24
9
224 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
The viewer will learn how to count occurrences of each item in an array.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now