在 PHP 中过滤字符串数组


Filtering string arrays in PHP

这使用 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 " &nbsp;";
}
    }

示例输出如下:

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 . ' &nbsp;' . "'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 . ' &nbsp;' . "'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>';
}
?>

> 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 . ' &nbsp;' . "'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>

您的数据结构在最后一个答案和这个答案之间的某个时间点发生了变化。如果这种情况继续发生,这些将永远行不通。

<?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