仅显示 if 语句循环中的第一项

此代码:


require_once 'feed.php';


$title = 'Ev 134';


ob_start();

foreach(Feed('example.url') as $f ) {

    if (strpos($f->title, $title) !== false) {


        $green_color    = 'green';

        $orange_color   = 'orange';

        $red_color      = 'red';

        $closed_text    = 'closed';

        $maintenance_text   = 'maintenance';

        $exception_text = 'could be';


        if (strpos($f->title, $title) !== false){


            if(strpos($f->description, $closed_text) !== false){

                echo (strpos($f->description, $exception_text) === false) ?

                     '<span style="color:'.$red_color.';text-shadow: 2px 2px #a50000;">closed</span>' :

                    '<span style="color:'.$green_color.'">Open</span>' ;


            } else if(strpos($f->description, $maintenance_text) !== false){

                echo (strpos($f->description, $exception_text) === false) ?

                     '<span style="color:'.$orange_color.'">maintenance</span>' :

                     '<span style="color:'.$green_color.'">Open</span>' ;


            } else {

                echo '<span>Open</span>';

            }

        }

    }

}

$status = ob_get_contents();

ob_end_clean();


echo $status;

从道路输出当前天气报告。打开/关闭或维护。


我的问题:


可能会有可能导致或我不想要的输出。openopenmaintenanceopen


我试过:


if ($status = 'OpenOpen'){

  $status = 'Open';

}

但是对于所有可能的情况来说,它都很复杂和混乱+它不能很好地工作。


我想要什么:如果有多个报告只显示第一个,并在文本后面放*。


任何帮助都非常感谢!


jeck猫
浏览 111回答 1
1回答

largeQ

此代码进行了一些更改。这不是使用输出缓冲,而只是将值设置为全通。这允许您检查是否已设置以前的值并添加到末尾,或者如果它到达循环的末尾并且仍然为空,则可以设置打开的文本。$status*$status我还将静态文本分配移到了循环之外,因为您不需要每次都设置它们。最后你有两次,所以这删除了那个...if (strpos($f->title, $title) !== false)$green_color&nbsp; &nbsp; = 'green';$orange_color&nbsp; &nbsp;= 'orange';$red_color&nbsp; &nbsp; &nbsp; = 'red';$closed_text&nbsp; &nbsp; = 'closed';$maintenance_text&nbsp; &nbsp;= 'maintenance';$exception_text = 'could be';$status = "";$records = 0;foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=604') as $f ) {&nbsp; &nbsp; $records++;&nbsp; &nbsp; if (strpos($f->title, $title) !== false) {&nbsp; &nbsp; &nbsp; &nbsp; if(strpos($f->description, $closed_text) !== false){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If no previous value, set main text,otherwise add *&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( empty($status) )&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $status = (strpos($f->description, $exception_text) === false) ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<span style="color:'.$red_color.';text-shadow: 2px 2px #a50000;">closed</span>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : '<span style="color:'.$green_color.'">Open</span>' ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $status .= "*";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else if(strpos($f->description, $maintenance_text) !== false){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( empty($status) )&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $status = (strpos($f->description, $exception_text) === false) ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<span style="color:'.$orange_color.'">maintenance</span>' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<span style="color:'.$green_color.'">Open</span>' ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $status .= "*";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// If still empty, say openif ( empty ( $status ) ){&nbsp; &nbsp; $status = '<span>Open</span>';&nbsp; &nbsp; if ( $records > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; $status.="*";&nbsp; &nbsp; }}echo $status;您可以在设置橙色和红色部分时删除测试,如果 文本中有 ,则忽略该项目。(strpos($f->description, $exception_text) === false)$exception_text
打开App,查看更多内容
随时随地看视频慕课网APP