dimanche 29 mars 2015

Shorten multiple elseifs in PHP?

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

Aucun commentaire:

Enregistrer un commentaire