Solved

array to table?

Posted on 2010-08-24
9
223 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Closing Comment

by:chrisj1963
Comment Utility
that was it. thanks very much!
0
 
LVL 10

Expert Comment

by:honestman31
Comment Utility
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
Comment Utility
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
Comment Utility
"\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
Comment Utility
duh.... I've got it now.  thanks very much for your follow-up....:)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.

762 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

6 Experts available now in Live!

Get 1:1 Help Now