如何在特定关键字之前和之后截断字符串?

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


例子:


…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;

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


德玛西亚99
浏览 178回答 2
2回答

缥缈止盈

此代码将尝试返回一个 80 个字符的子字符串,其中搜索到的字符尽可能居中。<?php$keyword = 'FOO BAR';$truncateChar = '…';$truncateCharLength = mb_strlen($truncateChar);$truncateLength = 80;$tests = [&nbsp; &nbsp;'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',&nbsp; &nbsp;'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',&nbsp; &nbsp;'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',&nbsp; &nbsp;'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',&nbsp; &nbsp;'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',&nbsp; &nbsp;'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&nbsp; 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40',&nbsp; &nbsp;'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',&nbsp; &nbsp;'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',&nbsp; &nbsp;];&nbsp; &nbsp;$str = "";&nbsp; &nbsp;foreach($tests as $orig_str) {&nbsp; &nbsp; &nbsp; &nbsp;if ($position = mb_strpos($orig_str, $keyword) !== FALSE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$position = mb_strpos($orig_str, $keyword);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$start = max($position - ($truncateLength - strlen($keyword))/2,0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$str = substr($orig_str, $start, $truncateLength);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$str = strlen($str)==$truncateLength?$str:substr($orig_str, max(strlen($orig_str)-$truncateLength,0),$truncateLength);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (mb_strpos($orig_str, $keyword)!= mb_strpos($str, $keyword)) $str = $truncateChar.$str;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;echo $str, " ",&nbsp; "\n";&nbsp; &nbsp;}将返回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 2&nbsp;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 2&nbsp;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 2&nbsp;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 2&nbsp;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&nbsp; 23 2&nbsp;… 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&nbsp;… 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&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP