TrippedStackers
TrippedStackers

Reputation: 423

How do I make it not show dots less than x chars?

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

Answers (4)

Bartosz
Bartosz

Reputation: 1

Try to write your own sub string function: it can be somthing similar like this http://www.sranko.com/nwP3LFit

Upvotes: 0

Ja͢ck
Ja͢ck

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

This would do.

<?php echo strlen(stripslashes($row['news_title']))>20 ?substr(stripslashes($row['news_title']), 0, 20)."...":stripslashes($row['news_title']); ?>

Upvotes: 0

GautamD31
GautamD31

Reputation: 28753

Try like

<?php echo substr(stripslashes($row['news_title']), 0, 20);
      if(strlen($row['news_title']) > 20)
          echo ".."; 
?>

Upvotes: 3

Related Questions