twan twan - 3 months ago 13
PHP Question

Looping through exploded array

I want to loop through an array that is exploded, but I'm stuck on how to do it.

My code:

// Explode the fulltext on ||
$explode = explode('||', $contentcr[0]['fulltext']);

echo $explode[0];

// Explode fulltext after || on commas
$explode2 = explode(',', $explode[1]);

$explode3 = explode('-', $explode2[1]);


foreach($explode2 as $list){
$lijst .= '
<div class="row item wow fadeInUp animated" data-wow-duration="2s" style="visibility: visible; animation-duration: 2s; animation-name: fadeInUp;">
<div class="col-md-2">
<span class="icon-lamp px50 iconcolor"></span>
</div>
<div class="col-md-10 left">
<div class="title black bold">'.strip_tags($list).'</div>
<p>
'.strip_tags($list).'
</p>
</div>
</div>';
}


My foreach creates a list with 2 lines. From a wysiwyg editor a user types for example: 'firstline-secondline,firstline-secondline' needs to generate two elements with two lines. At the beginning the string is split on || to indicate a list is there, then that string is split at a comma, and finally the result of that is split on a dash (-).

How can I loop through the exploded array to get the desired result?

So again as example:

firstline-secondline,firstline-secondline


Needs to turn into:

<div class="col-md-10 left">
<div class="title black bold">First line</div>
<p>
Second line
</p>
</div>
<div class="col-md-10 left">
<div class="title black bold">First line</div>
<p>
Second line
</p>
</div>


What I have in my editor now as per request:

<p>&nbsp;</p>
<p>Voor de leesmeer</p>
<hr id="system-readmore" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas tortor erat, commodo eget diam quis, pulvinar gravida dui. Nunc et ante mi. Nullam in dictum neque, id porttitor felis. Nunc eu nulla id ipsum tempus vestibulum. Vivamus sed turpis id magna finibus maximus vitae eget velit. Fusce sollicitudin ut leo ac eleifend. Quisque eu vulputate quam. Fusce et iaculis nulla, vel consectetur urna. Proin a nisi eget elit tempus faucibus sed vitae turpis.</p>
<p>||</p>
<p>Loremipsum1-2e325423532regel,LoremIpsum2-2eregel, Loremipsum3-2ersdgsdgegel</p>


The editor adds paragraph tags automatically so you can ignore those.

Answer

I am not totally sure of the shape of your input but it seems to me you are ignoring the explosion on hyphen and doing it in the wrong place

// Explode the fulltext on || 
$explode = explode('||', $contentcr[0]['fulltext']);

echo $explode[0];

// Explode fulltext after || on commas
$explode2 = explode(',', $explode[1]);

foreach($explode2 as $list){

    $lines = explode('-', $list);

    $lijst .= ' 
    <div class="row item  wow fadeInUp animated" data-wow-duration="2s" style="visibility: visible; animation-duration: 2s; animation-name: fadeInUp;">
        <div class="col-md-2">
            <span class="icon-lamp px50 iconcolor"></span>
        </div>
        <div class="col-md-10 left">
            <div class="title black bold">'. lines[0] .'</div>
            <p>
                '. $lines[1] .'
            </p>
        </div>
    </div>';
}