0

I am struggling to get the value of an element in a particular array. I would like to get the value of the "logo" in this case to return "Logo Google 2013 Official.svg" from the code below. Any help is greatly appreciated.

<html>
<head>
</head>
<body>

<html>

<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="google" />
<input type="submit" value="Submit">
</form>

<?php

if (isset($_POST['q'])) {
$search = $_POST['q'];


$url_2 = 
"http://en.wikipedia.org/w/api.php?   
action=query&prop=revisions&rvprop=content&format=json&titles=$search&rvsection=0&continue=";
$res_2 = file_get_contents($url_2);
$data_2 = json_decode($res_2);


?>

<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>
<?php foreach ($data_2->query->pages as $r): 

?>

<li>

<?php foreach($r->revisions[0] as $a); 
echo $a; ?>

</li>
<?php endforeach; ?>
</ol>

<?php 
}
?>

</body>
</html>

The resulting $url_2 is http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=google&rvsection=0&continue=

2
  • Are you sure you have posted the whole code? I can't see any logo or other element holding that filename anywhere in this code. Commented Nov 24, 2014 at 1:54
  • @tkounenis Hi I'm positive - just press search and there in the resulting text array is "logo = Logo Google 2013 Official.svg". Technically I suppose it is in the Infobox element. Please let me know if you still can't see it Commented Nov 24, 2014 at 2:01

1 Answer 1

1

Use a regular expression to capture what you want:

<ol>
<?php foreach ($data_2->query->pages as $r): ?>
    <?php foreach($r->revisions[0] as $a): ?>
    <li>
        <?php
            preg_match_all('/ logo += +([^|]+)/', $a, $result, PREG_PATTERN_ORDER);
            echo trim($result[0][0]); // prints 'Logo Google 2013 Official.svg'
        ?>
    </li>
    <?php endforeach; ?>
<?php endforeach; ?>
</ol>
Sign up to request clarification or add additional context in comments.

5 Comments

excellent thanks very much! Sorry to be a pain I was wondering whether this can be generalised to return the logo value from the Infobox for any company on Wikipedia?
In practice, it will return everything it finds after the first occurance of " logo = " up until the first pipeline ('|') character. After a brief research, there is not always a "logo = " string in the output of your link, so it is not guaranteed that you will get a logo back.
Thank again in your research could you see a way of getting logo = string using a different link?
I am not sure if there is a consistent way to get this 100% automatically. I would try Google's Custom Search API and get the first image probably.
sorry to keep pestering but I've just tried the same script with "Yahoo!" which does have logo = string and the preg_match_all gives me 0 results. Any ideas?

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.