Александр Александр - 5 months ago 15
HTML Question

PHP explode function find last child

I have a php plugin that explodes one line into several lines.

$graph_lines = explode( ";", $content );
$output= '';
$output .= '<ul class="lpd-bullet-list';
if($image){
$output .= ' lpd-bl-custom-icon';
}
if($style){
$output .= ' '.esc_attr($style);
}
$output .= '">';

foreach ($graph_lines as $line) {
if($line){

$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ';';
$output .= '</li>';

}
}

$output .= '</ul>';
echo $output;


After each line in the end it set
;
Thats OK, but how to set
.
for last element? Is it possible?

Mey be i need to use
end()
function or another?

Help me please!

Answer

You can get the number of line sizeof($graph_lines) and create a counter in your foreach.

Exemple :

   $numberofline = sizeof($graph_lines);
    $i=0;
    foreach ($graph_lines as $line) {
        if($line){
            $i++;
            $endofline=";";
            if($i==$numberofline)
            {
                $endofline=".";
            }
            $output .= '<li>';
            $output .= $image;
            $output .= $line;
            $output .= $endofline;
            $output .= '</li>';

        }
    }