0

This is the code I currently have:

<?php
 echo file_get_contents("http://example.com/bin/serp.php?engine=google&phrase=stackoverflow&format=ARRAY");
?>

It is displaying this on my page:

Array
(
[0] => Array
    (
        [idx] => 0
        [title] => Stack Overflow
        [description] => A language-independent collaboratively edited question and answer site for programmers.
        [url] => http://stackoverflow.com/
    )

[1] => Array
    (
        [idx] => 1
        [title] => Stack Overflow - Wikipedia, the free encyclopedia
        [description] => Stack Overflow website logo.png &middot; Stack Overflow.png. Screenshot of Stack Overflow as of December 2011. Web address &middot; stackoverflow.com. Commercial? Yes.
        [url] => http://en.wikipedia.org/wiki/Stack_Overflow
    )
)

What do I need to change to have it displayed like this instead?

1.  <a href="http://stackoverflow.com/">Stack Overflow</a>
A language-independent collaboratively edited question and answer site for programmers.

2.  <a href="http://en.wikipedia.org/wiki/Stack_Overflow">Stack Overflow - Wikipedia, the free encyclopedia</a>
Stack Overflow website logo.png &middot; Stack Overflow.png. Screenshot of Stack Overflow as of December 2011. Web address &middot; stackoverflow.com. Commercial? Yes.

Any help with this would be highly appreciated.

6
  • That sounds like a third-party API. What does their documentation say? Commented Aug 7, 2014 at 6:55
  • It seems output of print_r. Is it right or it's your print_r ? Commented Aug 7, 2014 at 6:55
  • $result = file_get_contents(.....); foreach($result as $value) { do something with $value } Commented Aug 7, 2014 at 6:55
  • @hd1 I've probably tried 100 different things and nothing seems to work. I'm guessing that I simply don't understand how to pull arrays from pages and actually use their data. Commented Aug 7, 2014 at 7:04
  • @ÁlvaroG.Vicario This isn't a third party API, it's an older PHP script that I got awhile back and it doesn't look like the original author is active anymore. Commented Aug 7, 2014 at 7:05

2 Answers 2

1

The print_r() function is a debug helper. It isn't intended as serialisation format:

  • There aren't built-in parsers (and it isn't trivial to write a robust one)
  • It can cause data loss, e.g.:

    $data = array(true, 1, false, 0, '');
    print_r($data);
    
    Array
    (
        [0] => 1
        [1] => 1
        [2] => 
        [3] => 0
        [4] => 
    )
    

You have this in the URL:

format=ARRAY

Just look at whatever place you use $_GET['format']. You'll possibly have more useful formats to choose from. If you don't, it'll be trivial to implement a sensible format, e.g. JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

It looks like there is an option for JSON. Would posting that output make it easier? It doesn't matter to me which I use...
If you have to ask whether calling json_decode() is easier than writing a custom parser for a bogus pseudo-format, it means that I've failed to properly explain the problem :)
0

You can use the foreach control statement of PHP in order to achieve this, and here it goes:

Say that the $array is the resultant of file_get_contents and supposing that it holds the result to be displayed.

    <?php foreach($array as $arr){?>
     <a href="<?php echo $arr['url'];?>"><?php echo $arr['title'];?></a>
     <?php echo $arr['description']; ?>
    <?php } ?>

Hope this helps you....

1 Comment

I got this error with that code: [07-Aug-2014 02:01:31 America/Chicago] PHP Parse error: syntax error, unexpected $end in /home/site/public_html/folder/folder/index.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.