2010-07-01 84 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