Search code examples
phpwordpressstrpos

What is a cleaner way to show a unique icon for each string that exists in an array using PHP?


I have an array $description[0] that contains the following:

( [0] => Time: 08:00 AM - 09:00 PM Type of Ensemble: Dueling Pianos No. of Players: 2 Additional Musicians: Yes Wedding Ceremony: Yes Cocktail Music: Yes Dinner Music: Yes Evening Entertainment: Yes DJ Services: Yes Shells: Full Projector: Yes Uplights: Yes )

I'm using strpos to determine if certain values are contained and then showing a unique icon for each instance.

I feel like this is really tacky code. Certainly the array could be broken down into keys for each thing I'm checking. But that is not the point at this time.

Mainly, I want to clean up the strpos function. Is there a cleaner way to do this than what I have below?

<?php if (strpos($description[0], 'Shells: Full') !== false) { ?><img src="https://www.example.com/wp-content/uploads/2021/01/full-shells-icon.png" title="Full Shells"  /><?php   } ?>
<?php if (strpos($description[0], 'Uplights: Yes') !== false) { ?><img src="https://www.example.com/wp-content/uploads/2021/01/uplights-icon.png" title="Uplights" /><?php   } ?>
<?php if (strpos($description[0], 'Projector: Yes') !== false) { ?><img src="https://www.example.com/wp-content/uploads/2021/01/projector-icon.png" title="Projector" /><?php   } ?>
<?php if (strpos($description[0], 'Wedding Ceremony: Yes') !== false) { ?><img src="https://www.example.com/wp-content/uploads/2021/01/wedding-ceremony-icon.png" title="Wedding Ceremony" /><?php   } ?>
<?php if (strpos($description[0], 'Additional Musicians: Yes') !== false) { ?><img src="https://www.example.com/wp-content/uploads/2021/01/extra_musicians_icon.png" title="Additional Musicians" /><?php   } ?>

Solution

  • I would make an array of the option descriptors and their relevant images; then you can just iterate over the array to output all the required images for the given description:

    $options = array(
        'Shells: Full' => '<img src="https://www.example.com/wp-content/uploads/2021/01/full-shells-icon.png" title="Full Shells"  />',
        'Uplights: Yes' => '<img src="https://www.example.com/wp-content/uploads/2021/01/uplights-icon.png" title="Uplights" />',
        'Projector: Yes' => '<img src="https://www.example.com/wp-content/uploads/2021/01/projector-icon.png" title="Projector" />',
        'Wedding Ceremony: Yes' => '<img src="https://www.example.com/wp-content/uploads/2021/01/wedding-ceremony-icon.png" title="Wedding Ceremony" />',
        'Additional Musicians: Yes' => '<img src="https://www.example.com/wp-content/uploads/2021/01/extra_musicians_icon.png" title="Additional Musicians" />'
    );
    
    foreach ($options as $option => $icon) {
        if (strpos($description[0], $option) !== false) {
            echo $icon;
        }
    }
    

    Output (for your sample input):

    <img src="https://www.example.com/wp-content/uploads/2021/01/full-shells-icon.png" title="Full Shells"  />
    <img src="https://www.example.com/wp-content/uploads/2021/01/uplights-icon.png" title="Uplights" />
    <img src="https://www.example.com/wp-content/uploads/2021/01/projector-icon.png" title="Projector" />
    <img src="https://www.example.com/wp-content/uploads/2021/01/wedding-ceremony-icon.png" title="Wedding Ceremony" />
    <img src="https://www.example.com/wp-content/uploads/2021/01/extra_musicians_icon.png" title="Additional Musicians" />
    

    Demo on 3v4l.org