2010-07-01 62 views
7

这里是不是为我工作:PHP限制的文本字符串不包括html标签?

<?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; 
?> 

我想限制可见文本100个字符,但使用substr()也包括在限额不可见的文本(<a href="http://www.jackismydog.com"></a>),它占据了41那些可用的100个字符。

是否有限制文本的方法,以便链接中的单词“Jack”包含在极限中,但不包括<a href="http://www.jackismydog.com"></a>

编辑: 我要保持字符串中的链接,就不能指望它是对极限长度..

回答

4

截断在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

如果你想限制文本部分,你需要分析它,并选中限制自己。最简单的方法是:

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

如果文本是多字节,不要忘记用'mb_strlen'替换'strlen'。 – machineaddict 2014-08-04 12:38:41

2

不容易 - 当然你可以使用strip_tags脱htmlise字符串,但比,有没有简单的解决方法等。

+0

解决我的问题!谢谢:) – yanike 2011-07-01 13:51:05

3

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

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

1

你可以试试这个,对我的工作,如果没有标签是字符串$不同将有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; 
?> 
+0

$ stringnohtml = strlen的(striphtml);应该是$ stringnohtml = strlen($ striphtml); – raison 2014-06-08 20:51:10