2010-08-26 18 views
2

我想在HTML片段的特定位置插入一個閱讀更多HTML位 - 比如在第10個單詞後面。然後,我將通過JavaScript在「閱讀更多」之後隱藏文本,並且只在點擊時顯示它。如何在HTML片段中插入字符串

JavaScript部分沒問題。至於PHP部分。

起初看起來很簡單 - 但問題是標籤弄亂了字數和截斷。所以我需要關閉任何打開的標籤,關閉對方超出「閱讀更多」。

任何有識之士將不勝感激。

+0

什麼樣的標籤在html中? – hookedonwinter 2010-08-26 22:38:53

+0

你能告訴我們一些代碼嗎,所以我們知道你已經有了什麼? – 2010-08-26 22:41:31

回答

2

我不知道這個源碼究竟來自哪裏,但我一直在使用這段代碼,它非常出色。

/** 
* @desc Cut given plain/HTML text nicely 
* @param string text to cut 
* @param int approximetly length of desired text length 
* @param int optional length, how far text can variante from approximetly length 
* @param bool optional can we cut words 
* @param bool optional do we need to append three dots to the end of cutted text 
* @return string cutted text 
*/ 

function htmlSubstr($text, $approxLength, $lengthOffset = 20, $cutWords = FALSE, $dotsAtEnd = TRUE) { 
    mb_internal_encoding('UTF-8'); 
    // $approxLength: 
    // The approximate length you want the concatenated text to be 

    // $lengthOffset: 
    // The variation in how long the text can be in this example text 
    // length will be between 200 and 200-20=180 characters and the 
    // character where the last tag ends 
    // Reset tag counter & quote checker 
    $tag_counter = 0; 
    $quotes_on = FALSE; 

    // Check if the text is too long 
    if (mb_strlen($text) > $approxLength) { 
     // Reset the tag_counter and pass through (part of) the entire text 
     $c = 0; 
     for ($i = 0; $i < mb_strlen($text); $i++) { 
      // Load the current character and the next one 
      // if the string has not arrived at the last character 
      $current_char = mb_substr($text,$i,1); 
      if ($i < mb_strlen($text) - 1) { 
       $next_char = mb_substr($text,$i + 1,1); 
      } else { 
       $next_char = ""; 
      } 

      // First check if quotes are on 
      if (!$quotes_on) { 
       // Check if it's a tag 
       // On a "<" add 3 if it's an opening tag (like <a href...) 
       // or add only 1 if it's an ending tag (like </a>) 
       if ($current_char == '<') { 
        if ($next_char == '/') { 
         $tag_counter += 1; 
        } else { 
         $tag_counter += 3; 
        } 
       } 

       // Slash signifies an ending (like </a> or ... />) 
       // substract 2 
       if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2; 
       // On a ">" substract 1 
       if ($current_char == '>') $tag_counter -= 1; 
       // If quotes are encountered, start ignoring the tags 
       // (for directory slashes) 
       if ($current_char == '"') $quotes_on = TRUE; 
      } else { 
       // IF quotes are encountered again, turn it back off 
       if ($current_char == '"') $quotes_on = FALSE; 
      } 

      // Count only the chars outside html tags 
      if($tag_counter == 2 || $tag_counter == 0) $c++; 

      // Check if the counter has reached the minimum length yet, 
      // then wait for the tag_counter to become 0, and chop the string there 
      if ($c > $approxLength - $lengthOffset && $tag_counter == 0 && ($next_char == ' ' || $cutWords == TRUE)) { 
       $text = mb_substr($text,0,$i + 1); 
       if($dotsAtEnd){ 
        $text .= '...'; 
       } 

       return $text; 
      } 
     } 
    } 
    return $text; 
} 
+0

我在發佈我的問題後不久就意識到我應該實際輸出html中的全部內容,並且爲了漸進式增強的目的在javascript中做截斷部分。建議的答案有效,所以我會接受它。 SO是如此快速的社區! – pixeline 2010-08-26 22:51:29

相關問題