Reputation: 423
if less than x don't show a "..."
<?php echo substr(stripslashes($row['news_title']), 0, 20). ".."; ?>
I have it to show more than x if more than 20, but it shows "..." when there's 10 chars. Is there anyway I could have it not to show?
any tutorials?
Thanks!
Upvotes: 1
Views: 53
Reputation: 1
Try to write your own sub string function: it can be somthing similar like this http://www.sranko.com/nwP3LFit
Upvotes: 0
Reputation: 173642
You could use CSS tricks, but this would be the code for doing it server-side:
if (strlen($row['news_title']) <= 20) {
echo htmlspecialchars($row['news_title']);
} else {
echo htmlspecialchars(substr($row['news_title'], 0, 20)), '...';
}
Note that strlen()
counts bytes and not characters per se; this is important when you start working with Unicode, in which case you may want to consider using mb_strlen()
.
Btw, using stripslashes()
is somewhat of a red flag; if your quotes come out as escaped, the problem lies somewhere else and shouldn't be a problem of the presentation layer ... in fact, you should be using htmlspecialchars()
instead.
Upvotes: 2
Reputation: 68536
This would do.
<?php echo strlen(stripslashes($row['news_title']))>20 ?substr(stripslashes($row['news_title']), 0, 20)."...":stripslashes($row['news_title']); ?>
Upvotes: 0
Reputation: 28753
Try like
<?php echo substr(stripslashes($row['news_title']), 0, 20);
if(strlen($row['news_title']) > 20)
echo "..";
?>
Upvotes: 3