1 回答

TA貢獻2039條經驗 獲得超8個贊
此代碼進行了一些更改。
這不是使用輸出緩沖,而只是將值設置為全通。這允許您檢查是否已設置以前的值并添加到末尾,或者如果它到達循環的末尾并且仍然為空,則可以設置打開的文本。$status*$status
我還將靜態文本分配移到了循環之外,因為您不需要每次都設置它們。
最后你有兩次,所以這刪除了那個...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';
$status = "";
$records = 0;
foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=604') as $f ) {
$records++;
if (strpos($f->title, $title) !== false) {
if(strpos($f->description, $closed_text) !== false){
// If no previous value, set main text,otherwise add *
if ( empty($status) ) {
$status = (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 {
$status .= "*";
}
}
else if(strpos($f->description, $maintenance_text) !== false){
if ( empty($status) ) {
$status = (strpos($f->description, $exception_text) === false) ?
'<span style="color:'.$orange_color.'">maintenance</span>' :
'<span style="color:'.$green_color.'">Open</span>' ;
}
else {
$status .= "*";
}
}
}
}
// If still empty, say open
if ( empty ( $status ) ){
$status = '<span>Open</span>';
if ( $records > 0 ) {
$status.="*";
}
}
echo $status;
您可以在設置橙色和紅色部分時刪除測試,如果 文本中有 ,則忽略該項目。(strpos($f->description, $exception_text) === false)$exception_text
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報