So I want to display an image depending on what a string contains, and I have multiple elseifs? I have shortened it down a bit, but this is currently 50+ lines. I am thinking there must be a cleaner way to do this?
<?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';}
?>
This is one solution:
$imgsrc = 'default.png';
for ( $percent=100; $percent>0; $percent--) {
if(strpos($this->escape($title), $percent . '% off') !== false){
$imgsrc = $percent . 'percentoff.png';
break;
}
}