Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with php explode?

Tags:

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.

like image 234
fr3d Avatar asked Mar 24 '26 23:03

fr3d


2 Answers

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.

like image 166
Anik Avatar answered Mar 27 '26 13:03

Anik


I assume that $this->value is an array so you can get the countries one by one using foreach loop like below:

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

if it is string than you can use explode() function and get the countries as an array like:

<?php $countries = explode("," , $this->value); ?>

and pass this $countries array to foreach loop.

like image 21
Kaan Burak Sener Avatar answered Mar 27 '26 11:03

Kaan Burak Sener



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!