我正在努力获取特定数组中元素的值。我希望在这种情况下获得“徽标”的价值,从下面的代码中返回“Logo Google 2013 Official.svg”。 非常感谢任何帮助。
<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>
答案 0 :(得分:1)
使用正则表达式捕获您想要的内容:
<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>