esedic
1/10/2017 - 4:51 PM

Split long string with PHP

Split long string with PHP

<?php

$string ="Orci molestie accumsan vestibulum a vehicula vel porttitor arcu consequat suscipit mi imperdiet arcu eros consequat tortor cras adipiscing libero magna ad in.Dignissim lorem rutrum suspendisse ac diam dis himenaeos vestibulum a duis feugiat pellentesque";

// SPLIT STRING TO ARRAY 1
$string = str_split("Orci molestie accumsan vestibulum a vehicula vel porttitor arcu consequat suscipit mi imperdiet arcu eros consequat tortor cras adipiscing libero magna ad in.Dignissim lorem rutrum suspendisse ac diam dis himenaeos vestibulum a duis feugiat pellentesque",150);
foreach ($string as $item) {
    echo $item;
}

// SPLIT STRING TO ARRAY 2
$output[0] = substr($string, 0, 150);
$output[1] = substr($string, 150);

// WORDWRAP FUNCTION
$newtext = wordwrap($string, 155, "</span><span>");
echo '<span>'.$newtext.'</span>';

// SUBSTRING FUNCTION
$first100 = substr($string, 0, 100);
$theRest = substr($string, 100);
echo '<h1>'.$first100.'</h1>';
echo '<h4>'.$theRest.'</h4>';

// COMBINE EXPLODE WITH WORDWRAP
$x = 100;
$longString = 'Condimentum aliquet ridiculus nibh viverra parturient a mus eu a accumsan lacinia tempus parturient vivamus sapien cum suspendisse sed per eget lectus commodo consectetur per ac adipiscing porttitor facilisis.Molestie suspendisse molestie.';

$strlnt = mb_strlen($longString);
if(strlen($strlnt > $x)) {
    $lines = explode("\n", wordwrap($longString, $x));
    foreach ($lines as $line) {
        echo '<span>'.$line.'</span>'   ;
    }    
}