【问题标题】:Truncate content before search keyword in text在文本中搜索关键字之前截断内容
【发布时间】:2011-10-31 09:59:13
【问题描述】:

我使用下面的代码在我的文本中的第一个搜索关键字之前和之后截断我的内容(这是我的搜索页面),除了在截断开头将单词切成两半的代码之外,一切正常,它不会在截断末尾截断单词。

例子:

lients at the centre of the relationship and to offer a first class service to them, which includes tax planning, investment management and estate planning. We believe that our customer focused and...

(编辑:有时单词中缺少一个以上的字符)

您会看到它已经从“clients”中删除了“c”。它只发生在文本的开头而不是结尾。我怎样才能解决这个问题?我相信我已经成功了一半。到目前为止的代码:

function neatest_trim($content, $chars, $searchquery,$characters_before,$characters_after) {
            if (strlen($content) > $chars) {
                 $pos = strpos($content, $searchquery);
                 $start = $characters_before < $pos ? $pos - $characters_before : 0;
                $len = $pos + strlen($searchquery) + $characters_after - $start;
                $content = str_replace('&nbsp;', ' ', $content);
                $content = str_replace("\n", '', $content);
                $content = strip_tags(trim($content));
                $content = preg_replace('/\s+?(\S+)?$/', '', mb_substr($content, $start, $len));
                $content = trim($content) . '...';
                $content = strip_tags($content);
                $content = str_ireplace($searchquery, '<span class="highlight" style="background: #E6E6E6;">' . $searchquery . '</span>', $content);
            }
            return $content;
        }



 $results[] = Array(
  'text' => neatest_trim($row->content,200,$searchquery,120,80)
            );

【问题讨论】:

  • 内容的开头总是只有一个字符吗?
  • 并非总是如此。有时不止一个
  • 我想我明白了,你基本上是在搜索关键字之前留下 120 个字符,在搜索关键字之后留下 80 个字符,是吗?
  • 是的。我的问题是它在开头而不是结尾砍我的话。

标签: php preg-replace truncate str-replace


【解决方案1】:

您在开始时保留的 120 个字符不检查第 120 个字符是空格还是字母,无论如何都只是剪切那里的字符串。

我会进行此更改,以搜索距我们开始位置最近的“空间”。

$start = $characters_before < $pos ? $pos - $characters_before : 0;
// add this line:
$start = strpos($content, ' ', $start);
$len = $pos + strlen($searchquery) + $characters_after - $start;

这样$start 是空格的位置,而不是单词中的字母。

你的函数会变成:

function neatest_trim($content, $chars, $searchquery,$characters_before,$characters_after) {
    if (strlen($content) > $chars) {
    $pos = strpos($content, $searchquery);
    $start = $characters_before < $pos ? $pos - $characters_before : 0;
    $start = strpos($content, " ", $start);
    $len = $pos + strlen($searchquery) + $characters_after - $start;
    $content = str_replace('&nbsp;', ' ', $content);
    $content = str_replace("\n", '', $content);
    $content = strip_tags(trim($content));
    $content = preg_replace('/\s+?(\S+)?$/', '', mb_substr($content, $start, $len));
    $content = trim($content) . '...';
    $content = strip_tags($content);
    $content = str_ireplace($searchquery, '<span class="highlight" style="background: #E6E6E6;">' . $searchquery . '</span>', $content);
    }
    return $content;
  }

【讨论】:

  • 我仍然或多或少地得到相同的东西。任何其他想法,把我的头发拉出来。哈哈
  • 除非您不想从内容的开头截断任何内容,否则这应该可以,我已经在本地测试过,并且它不会在开头截断任何单词...
  • 几乎与一些文本分开工作。例如“唱个人资料任何英国”,是“上升个人资料”的想法吗?
  • 奇怪,我无法复制这个问题,必须尝试复制我在上面放置的完整功能并用它替换你的?我的所有测试都没有,即使是“冉冉升起的个人资料”,也会截断单词......
  • 删除 $content = str_replace("\n", '', $content);它似乎正在工作......
【解决方案2】:

为什么不使用替换正则表达式?

$result = preg_replace('/.*(.{10}\bword\b.{10}).*/s', '$1', $subject);

所以这将修剪关键字 'word'

前后 10 个字符的所有内容

解释:

# .*(.{10}\bword\b.{10}).*
# 
# Options: dot matches newline
# 
# Match any single character «.*»
#    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the regular expression below and capture its match into backreference number 1 «(.{10}\bword\b.{10})»
#    Match any single character «.{10}»
#       Exactly 10 times «{10}»
#    Assert position at a word boundary «\b»
#    Match the characters “word” literally «word»
#    Assert position at a word boundary «\b»
#    Match any single character «.{10}»
#       Exactly 10 times «{10}»
# Match any single character «.*»
#    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

所以这个正则表达式的作用是查找您指定的单词(并且仅查找该单词,因为它包含在 \b - 单词边界中)并且它还查找 ant 将单词之前的 10 个字符存储(包括该单词)为以及它后面的十个字符。您可以自己构造正则表达式,其中包含前后字符的变量,当然还有关键字。正则表达式也匹配其他所有内容,但替换只使用反向引用 $1,这是您想要的输出。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
相关资源
最近更新 更多