为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
德玛西亚99 2021-06-18 12:17:35
我有一个字符串,我想搜索一个关键字,该字符串应该在关键字前后被截断。例子:…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 stringif (mb_strlen($str) > $truncateLength) {    $str = mb_substr($str, 0, $truncateLength - $truncateCharLength) . $truncateChar;}echo $str;这是一个搜索结果。我不想显示整个文本。仅显示关键字的部分。有没有更好的性能方法来做到这一点?正则表达式例如?
查看完整描述

2 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

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


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

   }

将返回


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 

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 

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 

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 

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 2 

… 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 

… 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 


查看完整回答
反对 回复 2021-06-19
  • 2 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信