fr3d fr3d - 6 months ago 12
PHP Question

Working with php explode?

I've got a list of countries, sometimes the list contains just one country and sometimes more. This is my code:

<?php if($this->value): ?>
<a class="tag" href="{{env::url}}/business?land=<?php echo $this->value; ?>" title="<?php echo $this->value; ?>"><?php echo $this->value; ?></a>
<?php endif; ?>


The output right now is: "Germany,Austria,Switzerland".

I want to create a link for every country, how can I do that?

I hope you guys can help me.

Answer

I assume that you are getting comma separate list of countries inside $this->value.

We can use function like explode to split that string into array and then use foreach to loop through array and generate individual link

<?php if($this->value): ?>
    <?php
        $array = explode( ',', $this->value );
    ?>
    <?php foreach($array as $value): ?>
        <a class="tag" href="{{env::url}}/business?land=<?php echo $value; ?>" title="<?php echo $value; ?>"><?php echo $value; ?></a> - 
    <?php endforeach; ?>
<?php endif; ?>

I hope this answers your question.

Comments