2017-06-17 4 views
0

Facebookのshow moreのような機能を持たせようとしています。 文字列をトリムする場合:URLのある文字列のFacebookのような[詳細を表示]ボタン

  • 長さが200文字を超えています。
  • 5つ以上のものがあります。/n

それは簡単に聞こえると私はすでに初期の機能を持っている(つまり、長さだけでそれをしない、私はまだ/n出現を実装していない):

function contentShowMore($string, $max_length) { 
    if(mb_strlen($string, 'utf-8') <= $max_length) { 
     return $string; // return the original string if haven't passed $max_length 
    } else { 
     $teaser = mb_substr($string, 0, $max_length); // trim to max length 
     $dots = '<span class="show-more-dots"> ...</span>'; // add dots 
     $show_more_content = mb_substr($string, $max_length); // get the hidden content 
     $show_more_wrapper = '<span class="show-more-content">'.$show_more_content.'</span>'; // wrap it 

     return $teaser.$dots.$show_more_wrapper; // connect all together for usage on HTML. 
    } 
} 

問題は、文字列が含まれる場合がありますということですURLに変換されます。長さ、改行をチェックし、URLを切断しない機能的なshow-moreボタンを作る方法を見つける必要があります。

ありがとうございました!

例:

入力:contentShowMore("hello there http://google.com/ good day!", 20)

出力:私はしたい

hello there http://g 
<span class="show-more-dots"> ...</span> 
<span class="show-more-content">oogle.com/ good day!</span> 

出力:

hello there http://google.com/ 
<span class="show-more-dots"> ...</span> 
<span class="show-more-content"> good day!</span> 
+0

あなたの入力と予想される出力を共有できますか? –

+0

この関数は多くの人に実行されるため、特定の入力と出力はありません。しかし、私は例を挙げることができます。 –

+0

第二引数を '20'として渡してもらえますか、それをどうやって取得しているのか、私はあなたを理解しやすくするために、あなたを助けてくれるでしょうか? –

答えて

0

が解決策を見つけました!

function contentShowMore($string, $max_length, $max_newlines) { 

    $trim_str = trim($string); 

    if(mb_strlen($trim_str, 'utf-8') <= $max_length && substr_count($trim_str, "\n") < $max_newlines) { // return the original if short, or less than X newlines 
     return $trim_str; 
    } else { 
     $teaser = mb_substr($trim_str, 0, $max_length); // text to show 
     $show_more_content = mb_substr($trim_str, $max_length); 

     // the read more might have cut a string (or worse - an URL) in the middle of it. 
     // so we will take all the rest of the string before the next whitespace and will add it back to the teaser. 
     $content_parts = explode(' ', $show_more_content, 2); // [0] - before first space, [1] - after first space 
     $teaser .= $content_parts[0]; 

     if(isset($content_parts[1])) { // if there are still leftover strings, its on show more! :) 
      $show_more_content = $content_parts[1]; 
     } 

     // NOW WERE CHEKING MAX NEWLINES. 
     $teaser_parts = explode("\n", $teaser); // break to array. 
     $teaser = implode("\n", array_slice($teaser_parts, 0, $max_newlines)); // take the first $max_newlines lines and use them as teaser. 
     $show_more_content = implode("\n", array_slice($teaser_parts, $max_newlines)) . ' ' . $show_more_content; // connect the rest to the hidden content. 

     if(mb_strlen($show_more_content, "UTF-8") === 0) { 
      return $trim_str; // nothing to hide - return original. 
     } else { 
      $show_more_wrapper = '<span class="show-more-content">'.$show_more_content.'</span>'; 

      $dots = '<span class="show-more-dots"> ...</span>'; // dots will be visible between the teaser and the hidden. 
      $button = ' <span class="show-more">Show more</span>'; 

      return $teaser.$dots.$button.$show_more_wrapper; // connect ingredients 
     } 

    } 
} 
関連する問題