用PHP在textarea中打印用户输入的摘要

时间:2011-07-06 10:24:33

标签: php string

我有一个表单textearea字段,用户输入的摘要如下:

  

il est poli comej hhh kkbkbkbkb   jbbbjbjblb ljlllbbblblb bnlbggkgkgkjg   lkjhhlhlhlhlhlh fin。

我想输出两行以'...'结尾而不打印所有类似

  

il est poli

     

comej hhh

     

kkbbkbkb jbbbjblb ...

如何在php中处理这个?

2 个答案:

答案 0 :(得分:0)

$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;
  }
}

答案 1 :(得分:0)

<?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
?>

这可以是:

<?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],'...';
?>