2

The code below uses a Wikipedia API to return data. I would like to output a string containing the Industry however cannot understand why the preg_match_all does not match and return the string related to industry - in this example of UBS, I would like to see "industry =[[Banking]], [[Financial services]]" returned. This string can be seen when using print_r to output data.
I'm sure I'm misunderstanding or missing something simple. Please assist.

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

<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$search = ucwords($search);
$search = str_replace(' ', '_', $search);
$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);

?>

<?php foreach ($data_2->query->pages as $r): 

?>

<?php foreach($r->revisions[0] as $a); 
print_r($a);
if (preg_match_all('/|industry += (.*)/i', $a, $result)) {

$industry = trim($result[0][0]);

echo $industry;
} 

?>

<?php endforeach;

?>

<?php 
}
?>

</body>
</html>
  • Your code is full of mistakes, e.g. the second foreach loop won't loop. Also you only need to open – ksbg Aug 18 '15 at 07:41
  • @freegarden - related to what you said, is there some mistake in my code which is why my code isn't returning what I'm after? – Damien Rice Aug 18 '15 at 07:51
  • Try if [regex like this](https://regex101.com/r/cG7lD3/1) works: `\|industry\s*=\s*\K[^|]+` matches would be in `$result[0]`. You need to escape `|` it has a special meaning inside regex: pipe character (used for alternation) – Jonny 5 Aug 19 '15 at 03:50
  • Have you considered using Wikidata instead, to access Wikipedia's data in machine-readable format? See e.g. http://stackoverflow.com/a/31290824/1333493, http://stackoverflow.com/a/22178443/1333493. – Nemo Oct 11 '15 at 11:44

0 Answers0