Mik*_*ade 1 php if-statement strpos
所以我想根据字符串包含的内容显示图像,我有多个elseif?我把它缩短了一点,但这是目前50多行.我想有必要有一个更清洁的方法来做到这一点?
<?php if(strpos(' '.$this->escape($title).' ', '25% off')){$imgsrc = '25percentoff.png';}
elseif(strpos(' '.$this->escape($title).' ', '24% off')){$imgsrc = '24percentoff.png';}
elseif(strpos(' '.$this->escape($title).' ', '23% off')){$imgsrc = '23percentoff.png';}
elseif(strpos(' '.$this->escape($title).' ', '22% off')){$imgsrc = '22percentoff.png';}
elseif(strpos(' '.$this->escape($title).' ', '21% off')){$imgsrc = '21percentoff.png';}
elseif(strpos(' '.$this->escape($title).' ', '20% off')){$imgsrc = '20percentoff.png';}
elseif(strpos(' '.$this->escape($title).' ', '19% off')){$imgsrc = '19percentoff.png';}
else{$imgsrc = 'default.png';}
?>
Run Code Online (Sandbox Code Playgroud)
小智 5
这是一个解决方案:
$imgsrc = 'default.png';
for ( $percent=100; $percent>0; $percent--) {
if(strpos($this->escape($title), $percent . '% off') !== false){
$imgsrc = $percent . 'percentoff.png';
break;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |