开发者

Printing a summary entered by user in textearea in PHP

开发者 https://www.devze.com 2023-03-18 03:49 出处:网络
i have a form textearea field where a user enter a summary like: il est poli comej hhh kkbkbkbkb jbbbjbjblb 开发者_如何学Pythonljlllbbblblb bnlbggkgkgkjg

i have a form textearea field where a user enter a summary like:

il est poli comej hhh kkbkbkbkb jbbbjbjblb 开发者_如何学Pythonljlllbbblblb bnlbggkgkgkjg lkjhhlhlhlhlhlh fin.

I would like to output this in two lines ending with '...' without printing all like

il est poli

comej hhh

kkbbkbkb jbbbjblb...

How to handle this in php ?


<?php
$summary = explode($textarea,' '); //split user input in words
echo $summary [0], ' ',$summary [1], ' ',$summary [2]; //print first 3 words
echo '<br>'; //newline
echo $summary [3], ' ',$summary [4]; //print 2 more words
echo '<br>'; //newline
echo $summary [5], ' ',$summary [6]; //print 2 more words
echo '...'; //dots
?>

This can be:

<?php
$summary = explode($textarea,' '); //split user input in words
echo $summary [0], ' ',$summary [1], ' ',$summary [2],'<br>',$summary [3], ' ',$summary [4],'<br>', $summary [5], ' ',$summary [6],'...';
?>


$str = 'asdf asdf sadf asdf asf asdf asdf asdf sfd';
$cut_length = 100;
if (strlen($str) > $cut_length)
  $str = substr($str, 0, $cut_length) .'...';

$line_length = 50;
$str_words = explpode(' ', $str);
$len = 0;
$str = '';
foreach ($str_words as $word) {
  $str .= $word . ' ';
  $len += strlen($word);
  if ($len >= $line_length) {
    $str .= '<br/>';
    $len = 0;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消