如何截断特定关键字前后的字符串?

时间:2019-05-14 11:52:07

标签: php

我有一个字符串,我想搜索一个关键字,并且该字符串应在关键字前后被截断。

示例:

…erat, sed diam voluptua. At vero eos et FOO BAR et justo duo dolores et ea reb…

如果关键字位于开头,则文本不应被截断。

At vero eos et FOO BAR et justo duo dolores et ea reb, lorem ipsum dolor sit amet…

这是我的摘录。真的不是很好...

$keyword = 'FOO BAR';
$truncateChar = '…';
$truncateCharLength = mb_strlen($truncateChar);
$truncateLength = 80;
$str = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et FOO BAR et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';

// Find the position of the keyword
$position = mb_strpos($str, $keyword);

if (($position - ($truncateLength / 2)) < 0) {
    // if there are not enough  chars ($truncateLength/2) before the keyword,
    // don't truncate at the beginning.
    $str = mb_substr($str, 0);
} else {
    // truncate ($truncateLength/2) chars before the keyword
    $str = $truncateChar . mb_substr($str, $position - ($truncateLength / 2));
}

// if the string is longer than $truncateLength, than truncate the string
if (mb_strlen($str) > $truncateLength) {
    $str = mb_substr($str, 0, $truncateLength - $truncateCharLength) . $truncateChar;
}

echo $str;

用于搜索结果。我不想显示整个文本。仅显示关键字的部分。有没有更好的性能方法可以做到这一点?正则表达式,例如?

2 个答案:

答案 0 :(得分:0)

乍一看,像这样的东西可能会解决问题。.还没有测试,只是在我的头上,所以您可以尝试一下。.也许对其进行一些修改以满足您的要求,因为它可能当该单词被多次发现时就会遇到麻烦。Idk,它可能会帮助您开始:

    preg_match('/.{0,40}FOO BAR.{0,40}/im', 'At vero eos et FOO BAR justo duo dolores et ea reb, lorem ipsum dolor sit amet.', $match);
    echo $match ? '...' . reset($match) . '...' : '';

答案 1 :(得分:0)

此代码将尝试返回一个80个字符的子字符串,其中所搜索的字符应尽可能居中。

public function create(Request $request)
{
    $user = new \App\User;
    $user->name = $request->name;
    $user->role = $request->role;
    $user->email = $request->email;
    $user->password = bcrypt('$request->password');
    $user->remember_token = str_random(60);
    $user->save();

    return redirect('/user')->with('success','Success data update');
}

            public function update(Request $request,$id)
         {
    $user = \App\User::find($id);
    $user->update($request->all());
    return redirect('/user')->with('succses','Succsess data update');
}

将返回

<?php
$keyword = 'FOO BAR';
$truncateChar = '…';
$truncateCharLength = mb_strlen($truncateChar);
$truncateLength = 80;
$tests = [
   '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40',
   '0 1 2 3 4 5 6 7 8 9 FOO BAR 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40',
   'FOO BAR 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40',
   '0 1 2 3 4 FOO BAR 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40',
   '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40',
   '0 1 2 3 4 5 6 7 8 9 10 11 12 13 FOO BAR 14 15 16 17 18 19 20 21 22 FOO BAR  23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40',
   '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 FOO BAR 35 36 37 38 39 40',
   '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 FOO BAR 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54',
   ];
   $str = "";
   foreach($tests as $orig_str) {
       if ($position = mb_strpos($orig_str, $keyword) !== FALSE) {
           $position = mb_strpos($orig_str, $keyword);
           $start = max($position - ($truncateLength - strlen($keyword))/2,0);
           $str = substr($orig_str, $start, $truncateLength);
           $str = strlen($str)==$truncateLength?$str:substr($orig_str, max(strlen($orig_str)-$truncateLength,0),$truncateLength);
           if (mb_strpos($orig_str, $keyword)!= mb_strpos($str, $keyword)) $str = $truncateChar.$str;
       }
       echo $str, " ",  "\n";
   }