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!
Try like
<?php echo substr(stripslashes($row['news_title']), 0, 20);
if(strlen($row['news_title']) > 20)
echo "..";
?>
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.
这样就可以了。
<?php echo strlen(stripslashes($row['news_title']))>20 ?substr(stripslashes($row['news_title']), 0, 20)."...":stripslashes($row['news_title']); ?>
尝试编写自己的子字符串函数:它可能像这样http://www.sranko.com/nwP3LFit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.