doucuoyan0426 2014-07-03 19:34
浏览 35

如何在PHP中将字符串截断为前n个单词

I would like to truncate a very long string, formatted via html elements.

I need the first 500 words (somehow I have to avoid html tags <p>, <br> while my function truncating the string), but in the result I have to keep/use those html elements because the result also should be formatted by html tags like the "original whole" text.

What's the best way to truncate my string?

Example:

Original text

> <p><a href="/t/the-huffington-post">The Huffington Post</a> (via <a
> href="/t/daily-mail">Daily Mail</a>) is reporting that <a
> href="/t/misty">Misty</a> has been returned to a high kill shelter for
> farting too much! She appeared on Greenville County Pet Rescue’s
> “urgent” list, which means if she doesn’t get readopted, she will be
> euthanized!</p>

I need the first n words (n=10)

>  <p><a href="/t/the-huffington-post">The Huffington Post</a> (via <a
> href="/t/daily-mail">Daily Mail</a>) is reporting that.. </p>
  • 写回答

2条回答 默认 最新

  • dragon8837 2014-07-03 19:54
    关注

    A brute force method would be to just split all elements on blanks, then iterate over them. You count only non-tag elements up to a maximum, while you output tags nonetheless. Something along these lines:

    $string = "your string here";
    $output = "";
    $count = 0;
    $max = 10;
    $tokens = preg_split('/ /', $string);
    foreach ($tokens as $token)
    {
      if (preg_match('/<.*?>/', $token)) {
        $output .= "$token ";
      } else if ($count < $max) {
        $output .= "$token ";
        $count += 1;
      }
    }
    print $output;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100