Reputation: 83
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"> <?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.
Upvotes: 1
Views: 3794
Reputation: 475
$newstext = substr($row_posts['tekst'], 0, strrpos(substr($row_posts['tekst'], 0, 20), ' '));
The above code will truncate any string to the nearest whole word, while staying under the maximum string length.
Upvotes: 1
Reputation: 1791
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); ?>
Upvotes: 1