-1

table name - animals column name id, story

I am inserting certain amount of data in story column.

But I need to display only first 20 characters in row. plz suggest me how to display only specific amount of characters.

below is my code... to echo rows.

<?php   foreach($rows as $row){ ?>   

         <?php echo htmlspecialchars_decode($row['story']); ?>

      <?php } ?>

1 Answers1

0

You can use the explode function, and itarate thorugh words:

foreach ($rows as $row) {
    $line = htmlspecialchars_decode($row['story']);
    $words = explode(" ", $line);
    $newWords = array();
    for ($i = 0; $i < 20; $i++) {
        if (isset($words[$i])) {
            $newWords[] = $words[$i];
        }
    }
    echo join(" ", $newWords)."<br>";
}
vaso123
  • 12,347
  • 4
  • 34
  • 64