Search code examples
phpmysqlcharacterlimitdreamweaver

How do i limit characters echoed from database?


I've made a "news" section on my website, and I'm trying to make a little box on my template to the right which echos latest news (title, text and author). My problem is that I only wish to echo about 20 characters from "text" in the little box.

I'm using dreamweaver, and i'm horrible at php. Here's the code. It's the "tekst" field i wan't to limit to 20 characters.

<?php do { ?>
<h1><?php echo $row_posts['tittel']; ?></h1>
<p class="posttekst">&nbsp;<?php echo $row_posts['tekst']; ?></p>
<p><em><?php echo $row_posts['forfatter']; ?> <?php echo $row_posts['dato']; ?></em></p>
<hr />
<?php } while ($row_posts = mysql_fetch_assoc($posts)); ?>

It's in norwegian - tittel : title, tekst: text, forfatter: author, dato:date.

Thanks for any help.


Solution

  • PHP's substr() is what you're looking for: http://www.php.net/manual/en/function.substr.php

    <?php echo substr($row_posts['tekst'], 0, 20); ?>