Cut strings short php

How would you divide the Cutting string so that it does not go to the next line in the div tag. For example, my line messagecontained the following:

We prefer questions that can be answered rather than just discussed. Provide detailed information. Write clearly and simply. If your question is about this site, ask for it instead of meta.

And I want it to display as

We prefer questions that can be answered rather than just discussed. Provide ... Read More

I think the string reduction is reduced using PHP, but then you have the following problem.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ... More

You can see that the above line can still be expanded. Any way to do this or not, thanks in Advance

+2
source share
11 answers

Basically, the following works: the main problem is that the notification "... Read more" may hide part of the message. It also uses a bunch of presentation elements and requires JS. It makes me feel unclean.

The outermost element (of the .string class) is used to set the overall size .. text contains the text to display. Between .string and .text in the child hierarchy there is an element with a large width. This puts all the text in the .text on one line. JS is used to show or hide the message "... Read more".

<style type="text/css">
  .string {
    height: 1em;
    width: 25%;
    position: relative; /* create containing block for children */
    border: 1px solid black;
    overflow: hidden;
    background: white; /* or whatever, as long as it not "transparent". */
  }
  .string .line {
    width: 5000px; /* long enough for you? */
  }
  .string .notice {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1;
    display: none;
    background: inherit; /* so that the notice will completely cover up whatever beneath */
  }
</style>

<div class="string">
  <div class="line"><span class="text">We prefer questions that can be answered, 
    not just discussed. Provide details. Write clearly and simply. If your 
    question is about this website, ask it on meta instead.</span></div>
</div>
<hr />
<div class="string">
  <div class="line"><span class="text">Lorem ipsum dolor sit amet.</span></div>
</div>

<script>
  function isOverflowed(elt) {
      return elt.offsetWidth > elt.parentNode.parentNode.clientWidth;
  }
  function getNotice(textElt) {
       try {
           return (textElt.parentNode.parentNode.getElementsByClassName('notice'))[0];
       } catch (e) {
           return {style: {}};
       }
  }
  function show(elt) {
      elt.style.display = 'block';
  }
  function hide(elt) {
      elt.style.display = 'none';
  }
  function showReadMoreNotices() {
    var text=document.getElementsByClassName('text');
    for (var i=0; i<text.length; ++i) {
      if (isOverflowed(text[i])) {
          show(getNotice(text[i]));
      } else {
          hide(getNotice(text[i]));
      }
    }
  }

  var strings = document.getElementsByClassName('string');
  var notice = document.createElement('div');
  notice.appendChild(document.createTextNode('\u2026Read more'));
  notice.className = 'notice';
  for (var i=0; i<strings.length; ++i) {
      strings[i].appendChild(notice.cloneNode(true));
  }

  showReadMoreNotices();
  /* use your favorite event registration method here. Not that the traditional 
   * model is my favorite, it just simple.
   */
  window.onresize = showReadMoreNotices;
</script>
+1
source

How can you shorten the line already answered, but your main question:

Cutting string short , div

.

:

iiiiiiiiii

wwwwwwwwww

?

, ​​ , :

iiiiiiiiii
wwwwwwwwww
+4

PHP, wordwrap :

function truncate_to_length($text, $length, $text_if_longer = "...") 
{
    if (strlen($text) > $length) 
    {
        $text = wordwrap($text, $length);
        $text = substr($text, 0, strpos($text, "\n"));
        return $text . $text_if_longer;
    }
    return $text;
}
+4

, , .

# = - ( "... " ). .

Jacob

+1

, http://snippetdb.com/php/truncate-string:

function Truncate ($str, $length=10, $trailing='...')  
{ 
      // take off chars for the trailing 
      $length-=strlen($trailing); 
      if (strlen($str) > $length)  
      { 
         // string exceeded length, truncate and add trailing dots 
         return substr($str,0,$length).$trailing; 
      }  
      else  
      {  
         // string was already short enough, return the string 
         $res = $str;  
      } 

      return $res; 
} 
+1

Oooo -, , ... Id

div.string{

    height:15px;        
    overflow:hidden;
}

, , .. - . , :

iiiiiiiiiiiiii

wwwwwwwwwwwwww

+1

, smarty, .

function str_short($string, $length = 80, $etc = '...',
                              $break_words = false, $middle = false)
{
if ($length == 0)
    return '';

if (strlen($string) > $length) {
    $length -= min($length, strlen($etc));
    if (!$break_words && !$middle) {
        $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
    }
    if(!$middle) {
        return substr($string, 0, $length) . $etc;
    } else {
        return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
    }
} else {
    return $string;
}

}

+1

. , , . , ? , , . , ...

0

PHP ( , , . , , span , : hidden.

0

, True- , imagettfbbox() GD . , div.

, , . .

<?
    $width = 280;   // max. width in pixels that the text should have
    $font = 'C:\Windows\Fonts\verdana.TTF';
    $text = 'The quick brown fox jumps over the lazy dog';

    function getWidth($text) {
        list($x1, , $x2) = imagettfbbox(12, 0, $font, $text);
        return $x2-$x1;
    }

    while (getWidth($text.'...') > $width) $text = substr($text, 0, -1);
?>

<style type='text/css'>
    #my_div {
        font-family: Verdana; 
        font-size: 12pt; 
        border: 1px solid black; 
        width: 280px; 
        padding: 0
    }
</style>

<div id='my_div'>
    <?=$text?>...
</div>
0

, , CSS :

div.string {
    word-wrap: break-word;
}
0

Source: https://habr.com/ru/post/1730647/


All Articles