【问题标题】:PHP limit text string NOT including html tags?PHP限制文本字符串不包括html标签?
【发布时间】:2010-07-01 21:00:39
【问题描述】:

这对我不起作用:

<?php
 $string = 'I have a dog and his name is <a href="http://www.jackismydog.com">Jack</a> and I love him very much because he\'s my favorite dog in the whole wide world and nothing could make me not love him, I think.';
 $limited = substr($string, 0, 100).'...';
 echo $string;
?>

我想将 VISIBLE 文本限制为 100 个字符,但使用 substr() 还包括限制中的不可见文本(&lt;a href="http://www.jackismydog.com"&gt;&lt;/a&gt;),它占用了可用 100 个字符中的 41 个。

有没有办法限制文本,使链接中的“Jack”一词包含在限制中,但不包括&lt;a href="http://www.jackismydog.com"&gt;&lt;/a&gt;

编辑: 我想将链接保留在字符串中,只是不将其长度计入限制..

【问题讨论】:

    标签: php substr


    【解决方案1】:

    在 HTML 代码中截断单词的函数:

    //+ Jonas Raoni Soares Silva
    //@ http://jsfromhell.com
    function truncate($text, $length, $suffix = '&hellip;', $isHTML = true) {
        $i = 0;
        $simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
        $tags = array();
        if($isHTML){
            preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
            foreach($m as $o){
                if($o[0][1] - $i >= $length)
                    break;
                $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
                // test if the tag is unpaired, then we mustn't save them
                if($t[0] != '/' && (!isset($simpleTags[$t])))
                    $tags[] = $t;
                elseif(end($tags) == substr($t, 1))
                    array_pop($tags);
                $i += $o[1][1] - $o[0][1];
            }
        }
    
        // output without closing tags
        $output = substr($text, 0, $length = min(strlen($text),  $length + $i));
        // closing tags
        $output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
    
        // Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
        $pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
        // Append closing tags to output
        $output.=$output2;
    
        // Get everything until last space
        $one = substr($output, 0, $pos);
        // Get the rest
        $two = substr($output, $pos, (strlen($output) - $pos));
        // Extract all tags from the last bit
        preg_match_all('/<(.*?)>/s', $two, $tags);
        // Add suffix if needed
        if (strlen($text) > $length) { $one .= $suffix; }
        // Re-attach tags
        $output = $one . implode($tags[0]);
    
        //added to remove  unnecessary closure
        $output = str_replace('</!-->','',$output); 
    
        return $output;
    }
    

    来源:http://snippets.dzone.com/posts/show/7125

    【讨论】:

    【解决方案2】:

    最简单的方法是将其实际解析为 DOM 结构。您可以为此使用DOMDocument。然后,您可以简单地浏览元素并对内容进行任何更改。

    另一种方法是进行两次正则表达式搜索和替换 - 首先使用正则表达式查找标签内容,然后使用正则表达式将内容替换为缩短的内容。这可以通过您常用的 preg_* 函数来实现。

    【讨论】:

      【解决方案3】:

      不容易——您当然可以使用strip_tags 对字符串进行反html,但除此之外没有简单的解决方法。

      【讨论】:

      • 解决我的问题!谢谢:)
      【解决方案4】:

      如果要对文本部分进行限制,则需要自己解析并检查限制。最简单的方法是:

      if ( strlen(strip_tags($string)) > 100 )
      {
          // the url inside $url is too big
      }
      else
      {
          // the url inside $url fits
      }
      

      【讨论】:

      • 如果文本是多字节的,不要忘记将strlen 替换为mb_strlen
      【解决方案5】:

      你可以试试这个,如果字符串中没有标签,对我有用 $differ 的值为 0 给 $stringsize 你的原始值为 100

        <?php
      $string = 'I have a dog and his name is <a href="http://www.jackismydog.com">Jack</a> and I love him very much because he\'s my favorite dog in the whole wide world and nothing could make me not love him, I think.';
      
      $stringall=strlen($string);
      $striphtml = strip_tags($string);
      $stringnohtml=strlen(striphtml);
      $differ=($stringall-$stringnohtml);
      $stringsize=($differ + 100);
      $limited = substr($string, 0, $stringsize).'...';
      echo $limited;
      ?>
      

      【讨论】:

      • $stringnohtml=strlen(striphtml);应该是 $stringnohtml=strlen($striphtml);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多