Search code examples
phphtmlsubstringsubstrstrlen

How to add <br> in string with substr()


For the following code :

<?php
$word = 'SEKISUI';

// echo substr($word,0,-6)."<br>";
$length = (strlen($word)+1)-14;
$urut = 0;
  for($i=$length;$i<1;$i++){
    echo substr($word,$urut,$i).'<br>';
//    echo $urut."-".$i."-".'<br>'; // Check Value
    $urut++;
  }

?>

Result :

S
E
K
I
S
U

why the letter "i" doesn't appear?
what is wrong with my code?
The result should look like:

S
E
K
I
S
U
I

Thank you for your attention...


Solution

  • I don't know if you NEED to use a 'for loop', but there is a better way to split a string into single characters.

    Once you have the array you can do any operation to it, also join() the items in the array with a "\< br>" separator.

    Try the following:

    $word = 'SEKISUI';
    $result = str_split($word);
    $altogether = join("<br>",$result);