Это использует Wolfram Alpha API для получения самолетов поблизости, а затем отображает его. Как удалить направление плоскости и фразу "Наклонное расстояние"?
Мой код (PHP):
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<style>
body {
background-color: rgba(255, 255, 255, 0.3);
font-family: 'Open Sans', sans-serif;
text-align:center;
}
</style>
</head>
<?php
$url = 'http://api.wolframalpha.com/v2/query?input=planes+seen+from+dallas&appid=2UJ62E-Q6RT3T89P8';
$parser = new XMLReader;
$parser->open($url);
while ($parser->read()) {
if ($parser->nodeType === XMLReader::ELEMENT) {
while ($parser->name === 'pod' && $parser->getAttribute('title') !== 'Result')
$parser->next('pod'); // jump to the next pod node
if ($parser->name === 'plaintext') {
$str = $parser->readString();
$parser->close();
break;
}
}
}
$lines = explode("\n", $str);
$result = array();
foreach ($lines as $line) {
$fields = explode(' | ', $line);
$flight = array_shift($fields);
$flight = $flight . "<hr>"; //DELETE IF DOESN'T WORK
if ($flight === '')
$cols = $fields;
elseif (isset($fields[1])) {
$result[$flight][$cols[0]] = $fields[0];
$result[$flight][$cols[1]] = $fields[1];
}
}
foreach($result as $key=>$value)
{
echo $key;
foreach($value as $value1){
echo $value1;
echo " ";
}
}
Пример вывода ниже:
slant distance ENY flight 3278
14 miles NNW Frontier Airlines flight 72
44 miles N American Airlines flight 1241
15 miles NW American Airlines flight 396
23 miles W Atlantic Southeast Airlines flight 6104
49 miles SSE
Я хочу, чтобы это выглядело так:
Frontier flight 3278
Airlines flight 72
American Airlines flight 1241
American Airlines flight 396
Atlantic Southeast Airlines flight 6104
Как это?
foreach($value as $value1){
if(preg_match('~(flight\s+\d+)~mis', $value1, $flightdata) || preg_match('~\s+(.*?\s+Airlines)\s+~mis', $value1, $airlinedata)) {
if(!empty($flightdata[1])) {
echo $flightdata[1];
}
if(!empty($airlinedata[1])) {
echo $airlinedata[1];
}
echo $value1 . ' ' . "\n";
}
Вы не упомянули hr на своем желаемом выходе, но это должно быть достаточно легко для вас.
Обновлено (непроверено, потому что я удалил файл после ответа):
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<style>
body {
background-color: rgba(255, 255, 255, 0.3);
font-family: 'Open Sans', sans-serif;
text-align:center;
}
</style>
</head>
<?php
$url = 'http://api.wolframalpha.com/v2/query?input=planes+seen+from+dallas&appid=2UJ62E-Q6RT3T89P8';
$parser = new XMLReader;
$parser->open($url);
while ($parser->read()) {
if ($parser->nodeType === XMLReader::ELEMENT) {
while ($parser->name === 'pod' && $parser->getAttribute('title') !== 'Result') {
$parser->next('pod'); // jump to the next pod node
}
if ($parser->name === 'plaintext') {
$str = $parser->readString();
$parser->close();
break;
}
}
}
$lines = explode("\n", $str);
$result = array();
foreach ($lines as $line) {
$fields = explode(' | ', $line);
$flight = array_shift($fields);
$flight = $flight . "<hr>"; //DELETE IF DOESN'T WORK
if ($flight === '') {
$cols = $fields;
} elseif (isset($fields[1])) {
$result[$flight][$cols[0]] = $fields[0];
$result[$flight][$cols[1]] = $fields[1];
}
}
foreach($result as $key=>$value) {
foreach($value as $value1){
if(preg_match('~(flight\s+\d+)~mis', $value1, $flightdata) || preg_match('~\s+(.*?\s+Airlines)\s+~mis', $value1, $airlinedata)) {
if(!empty($flightdata[1])) {
echo $flightdata[1];
}
if(!empty($airlinedata[1])) {
echo $airlinedata[1];
}
echo $value1 . ' ' . "\n";
}
}
}
?>
olivr3000,
Как-то мне удавалось испортить мое редактирование выше и не включало код, который, наконец, работал для моего тестирования.
Вот код с моими изменениями. Вы можете увидеть его в действии по адресу http://hdreports.com/test/testjson.php, а источником является http://hdreports.com/test/testjson.txt
Вот оно и работает. Извините за задержку в получении его здесь.
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<style>
body {
background-color: rgba(255, 255, 255, 0.3);
font-family: 'Open Sans', sans-serif;
text-align:center;
}
</style>
</head>
<?php
$url = 'http://api.wolframalpha.com/v2/query?input=planes+seen+from+dallas&appid=2UJ62E-Q6RT3T89P8';
$parser = new XMLReader;
$parser->open($url);
while ($parser->read()) {
if ($parser->nodeType === XMLReader::ELEMENT) {
while ($parser->name === 'pod' && $parser->getAttribute('title') !== 'Result') {
$parser->next('pod'); // jump to the next pod node
}
if ($parser->name === 'plaintext') {
$str = $parser->readString();
$parser->close();
break;
}
}
}
$lines = explode("\n", $str);
$result = array();
foreach ($lines as $line) {
$fields = explode(' | ', $line);
$flight = array_shift($fields);
if ($flight === '') {
$cols = $fields;
} elseif (isset($fields[1])) {
$result[$flight][$cols[0]] = $fields[0];
$result[$flight][$cols[1]] = $fields[1];
}
}
foreach($result as $key=>$value) {
echo $key.'<BR>';
}
?>
Ваша структура данных изменилась в какой-то момент между последним ответом и этим. Если это будет продолжаться, это никогда не сработает.
<?php
$url = 'http://api.wolframalpha.com/v2/query?input=planes+seen+from+dallas&appid=2UJ62E-Q6RT3T89P8';
$parser = new XMLReader;
$parser->open($url);
while ($parser->read()) {
if ($parser->nodeType === XMLReader::ELEMENT) {
while ($parser->name === 'pod' && $parser->getAttribute('title') !== 'Result') {
$parser->next('pod'); // jump to the next pod node
}
if ($parser->name === 'plaintext') {
$str = $parser->readString();
$parser->close();
break;
}
}
}
$lines = explode("\n", $str);
foreach ($lines as $line) {
if(preg_match('~^(.*?)\s+(flight\s+\d+)~', $line, $matches)){
echo $matches[1] . ' ' . $matches[2] . "\n";
}
}
?>
Вывод через мою оболочку....
United Airlines flight 1274
Delta Air Lines flight 2389
Mesa Airlines flight 3734
United Airlines flight 569
Shuttle America flight 3473
United Airlines flight 1274
Delta Air Lines flight 2389
Mesa Airlines flight 3734
United Airlines flight 569
Shuttle America flight 3473
Olivr3000 здесь обновление. Я попытался отредактировать код Chris85, чтобы опубликовать это вчера, но мое редактирование не опубликовано. Я изменил окончательный foreach для вывода данных авиакомпании по вашему запросу
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<style>
body {
background-color: rgba(255, 255, 255, 0.3);
font-family: 'Open Sans', sans-serif;
text-align:center;
}
</style>
</head>
<?php
$url = 'http://api.wolframalpha.com/v2/query?input=planes+seen+from+dallas&appid=2UJ62E-Q6RT3T89P8';
$parser = new XMLReader;
$parser->open($url);
while ($parser->read()) {
if ($parser->nodeType === XMLReader::ELEMENT) {
while ($parser->name === 'pod' && $parser->getAttribute('title') !== 'Result') {
$parser->next('pod'); // jump to the next pod node
}
if ($parser->name === 'plaintext') {
$str = $parser->readString();
$parser->close();
break;
}
}
}
$lines = explode("\n", $str);
$result = array();
foreach ($lines as $line) {
$fields = explode(' | ', $line);
$flight = array_shift($fields);
$flight = $flight . "<hr>"; //DELETE IF DOESN'T WORK
if ($flight === '') {
$cols = $fields;
} elseif (isset($fields[1])) {
$result[$flight][$cols[0]] = $fields[0];
$result[$flight][$cols[1]] = $fields[1];
}
}
foreach($result as $key=>$value) {
foreach($value as $value1){
if(preg_match('~(flight\s+\d+)~mis', $value1, $flightdata) || preg_match('~\s+(.*?\s+Airlines)\s+~mis', $value1, $airlinedata)) {
if(!empty($flightdata[1])) {
echo $flightdata[1];
}
if(!empty($airlinedata[1])) {
echo $airlinedata[1];
}
echo $value1 . ' ' . "\n";
}
}
}
?>
который дает этот результат HTML
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<style>
body {
background-color: rgba(255, 255, 255, 0.3);
font-family: 'Open Sans', sans-serif;
text-align:center;
}
</style>
</head>
American Airlines flight 1046<BR>N929FD<BR>
ENY flight 3238<BR>
Southwest Airlines flight 2477<BR>
American Airlines flight 2352<BR>