在 PHP 中过滤字符串数组

Filtering string arrays in PHP

这使用 Wolfram Alpha API 获取附近的飞机,然后显示它。如何删除平面方向和短语 'Slant distance'?

我的代码(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 这是一个更新。我昨天尝试将 Chris85 代码编辑为 post,但我的编辑没有发布。我更改了最终的 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>

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";
    }
}
?>

通过我的shell输出...

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