PHP直接删除字符串中的单词



我正在编写一个搜索引擎。基本上,如果某个单词出现,我需要在该单词之后立即抓取并删除该单词。

如果"瑜伽"这个词出现了,我需要删除它后面的单词,这里是"垫子"。所以我会得到:

$sentence="I like yoga mats a lot.";
$word="mats";
$result=I like yoga a lot.

我看了一下strpos,但需要一句话。我也有preg_split来按名称删除单词,但我还需要按位置删除这个特定的单词。

$separate = preg_split('/s+/', $sentence);

既然"瑜伽"后面的单词并不总是垫子,我该怎么去掉它呢。我仍然非常需要这些词。

这个代码片段应该做您想要做的事情:

$words = explode(' ', $sentence);
foreach (array_keys($words, 'yoga') as $key) {
  unset($words[$key+1]);
}
$sentence = implode(' ', $words);

代码非常不言自明:用单词分隔句子,识别所有值为"yoga"的键,取消设置下一个单词,并从剩余单词中重新组合句子。

$sentence = "I like yoga mats a lot.";
$word = "yoga";
echo preg_replace('#(b' . preg_quote($word) . 'b)W*bw+b#U', '$1', $sentence);

但是下一个"单词"可以是"a"、"the"等等。为了跳过这些单词而不是"单词",应该创建列表并添加额外的操作。

ps:好的,regexp 的解释

#  - start of regexp
(  - start of capture  
 b - boundary of the word
 preg_quote($word)  - escaped word to find
 b - boundary of the word
) - close capture group
W* - any non-word characters
b - boundary of the next word
w+ - word characters
b - boundary
# - end of regexp
U - un-greedy modifier

并且匹配的内容被捕获组CCD_ 1 的内容替换

<?php
    $user_input = "tea";
    $sentence="I like tea mats a lot.";
    $word_to_remove = $user_input . " ";
    $offset = strlen( $word_to_remove );
    $start_pos = stripos( $sentence , $word_to_remove );
    $end_pos = stripos( $sentence , " ", $start_pos+$offset );
    $str_to_replace = trim( substr( $sentence , $start_pos+$offset, ($end_pos-$offset)-$start_pos ) );
    $new_sentence = str_replace( $str_to_replace, "", $sentence );
    $new_sentence = preg_replace( "/s+/", " ", $new_sentence);
    echo $new_sentence;
?>

相关内容

  • 没有找到相关文章

最新更新