问题
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>
来源:https://stackoverflow.com/questions/32066282/preg-match-all-not-returning-expected-values