【问题标题】:cut text after (x) amount of characters在 (x) 个字符后剪切文本
【发布时间】:2010-04-26 21:52:00
【问题描述】:

这是在 wordpress 中(不确定是否有所作为)

这点php输出帖子标题

<?php echo $data['nameofpost']; ?>

它是简单的文本,最长可达 100 个字符。我想要的是,如果输出的字符超过 20 长以显示 '...' 或者根本什么都没有。

谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用strlen 检查字符串长度后,使用substr

    $string = "This is a large text for demonstrations purposes";
    if(strlen($string) > 20) $string = substr($string, 0, 20).'...';
    echo $string;
    

    输出

    "This is a large text..."
    

    【讨论】:

      【解决方案2】:

      在单词末尾切断字符串的另一种方法是使用正则表达式。这一项设置为在 100 个字符处截断或在 100 个字符后最近的分词:

      function firstXChars($string, $chars = 100)
      {
          preg_match('/^.{0,' . $chars. '}(?:.*?)\b/iu', $string, $matches);
          return $matches[0];
      }
      

      【讨论】:

        【解决方案3】:
        <?php
            function abbreviate($text, $max) {
                if (strlen($text)<=$max)
                    return $text;
                return substr($text, 0, $max-3).'...';
            }
        ?>
        
        <?php echo htmlspecialchars(abbreviate($data['nameofpost'], 20)); ?>
        

        一个常见的改进是尝试在一个单词的末尾截断字符串:

                if (strlen($text)<=$max)
                    return $text;
                $ix= strrpos($text, ' ', $max-2);
                if ($ix===FALSE)
                    $text= substr($text, 0, $max-3);
                else
                    $text= substr($text, 0, $ix);
                return $text.'...';
        

        如果您使用 UTF-8 字符串,您可能希望使用字符串操作的 mb_ multibyte 版本来更适当地计算字符数。

        【讨论】:

          【解决方案4】:

          在你的主题文件中使用类似这样的东西 尝试使用&lt;div class="teaser-text"&gt;&lt;?php the_content_limit(100, ''); ?&gt;&lt;/div&gt;

          然后在functions.php文件中,使用这个

          function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '')
           {
          
              $content = get_the_content($more_link_text, $stripteaser, $more_file);
              $content = apply_filters('the_content', $content);
              $content = str_replace(']]>', ']]&gt;', $content);
              $content = strip_tags($content);
          
             if (strlen($_GET['p']) > 0) 
          {
          
                echo "<div>";
                echo $content;
                echo "</div>";
             }
             else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char )))
           {
          
                  $content = substr($content, 0, $espacio);
                  $content = $content;
                  echo "<div>";
                  echo $content;
                  echo "...";
                  echo "</div>";
             }
             else {
                echo "<div>";
                echo $content;
                echo "</div>";
             }
          }
          

          祝你好运:)

          【讨论】:

            【解决方案5】:
            if(count($data['nameofpost']) > 20)
            {
                echo(substr($data['nameofpost'], 0, 17)."...");
            }
            

            对于大于 20 个字符的 $data['nameofpost'],它将输出前 17 个加三个点 ...

            【讨论】:

            • 是的,但如果它更短,它不会输出任何东西。
            • else 综合症...我也是这个的受害者,;)
            • 我假设他会把 else 放在那里。 :p
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-14
            • 2017-02-25
            • 2011-05-04
            • 2015-07-29
            • 2012-12-29
            • 1970-01-01
            相关资源
            最近更新 更多