PHP remove next two words after a specific word
在PHP中,我如何删除特定单词preg_replace后面的两个单词?例如:字符串:lorem ipsum dolor sit amet,consetetur sadipscing eliter,sed diam.具体词:ipsum新弦:洛雷姆·伊普苏姆·阿美,康塞特·萨迪普辛格·艾丽特,塞德·戴姆。
这是我当前的代码:
1 2 3 4 | $txt ="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam. Specific word: ipsum"; $str= preg_replace('/\W\w+\s*(\W*)$/', '$1', $txt); echo $str; |
但它只是删除字符串的最后一个字。
谢谢你谨致问候
您可以使用
1 2 3 | function remove2w($anchor, $text, $number = 2) { return preg_replace(sprintf('/(?<=%s)(?: \w+){%s}/', $anchor, $number), '', $text); } |
输出:
1 2 | remove2w('ipsum', 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam.') >>> Lorem ipsum amet, consetetur sadipscing elitr, sed diam. |
。
另一种使用
1 2 3 4 5 6 7 8 |
结果
Lorem ipsum amet, consetetur sadipscing elitr, sed diam
号
演示
注意:如果找不到
1 2 3 4 5 | <?php $needle ="ipsum"; $haystack ="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam."; $pattern = sprintf('|(%s)\s+\w+\s+\w+|', $needle); var_dump(preg_replace($pattern, '$1', $haystack)); |
输出显然是:
1 | string(57)"Lorem ipsum amet, consetetur sadipscing elitr, sed diam." |
号
在这项任务中有许多考虑因素。
这是一个完整的针电池:(演示)
1 2 3 4 5 6 | $txt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam.'; $needles = str_word_count(strtolower($txt),1); foreach($needles as $needle){ echo"($needle) =>",preg_replace('~\b'.$needle.'\b\S*\K(?:\s\S+){0,2}~i','',$txt)," "; // use '(($0))'' as replacement to see the substring that is removed } |
。
输出:
1 2 3 4 5 6 7 8 9 10 | (lorem) => Lorem sit amet, consetetur sadipscing elitr, sed diam. (ipsum) => Lorem ipsum amet, consetetur sadipscing elitr, sed diam. (dolor) => Lorem ipsum dolor consetetur sadipscing elitr, sed diam. (sit) => Lorem ipsum dolor sit sadipscing elitr, sed diam. (amet) => Lorem ipsum dolor sit amet, elitr, sed diam. (consetetur) => Lorem ipsum dolor sit amet, consetetur sed diam. (sadipscing) => Lorem ipsum dolor sit amet, consetetur sadipscing diam. (elitr) => Lorem ipsum dolor sit amet, consetetur sadipscing elitr, (sed) => Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed (diam) => Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam. |
。
分解:
1 2 3 4 5 6 7 | ~ #pattern delimiter \b'.$needle.'\b #match needle as a whole word \S* #match zero or more trailing character as long as first character is a non-word character. This may be replaced with [[:punct:]]+ if more desirable/accurate \K #restart fullstring match (?:\s\S+){0,2} #match zero, one or two sequences of: a whitespace character followed by one or more non-whitespace characters ~ #pattern delimiter i #case-insensitive pattern modifier |