简体   繁体   English

如何使其不显示少于x个字符的点?

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

if less than x don't show a "..." 如果小于x,则不显示“ ...”

<?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. 如果大于20,我可以显示多于x,但是当有10个字符时,它显示为“ ...”。 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: 您可以使用CSS技巧 ,但这将是在服务器端执行此操作的代码:

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; 注意, strlen()本身计算字节而不是字符; this is important when you start working with Unicode, in which case you may want to consider using mb_strlen() . 当您开始使用Unicode时,这一点很重要,在这种情况下,您可能需要考虑使用mb_strlen()

Btw, using stripslashes() is somewhat of a red flag; 顺便说一句,使用stripslashes()有点stripslashes() 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. 如果引号转义,问题就出在别处,这不应该是表示层的问题……实际上,您应该使用htmlspecialchars()代替。

这样就可以了。

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

尝试编写自己的子字符串函数:它可能像这样http://www.sranko.com/nwP3LFit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM