0

I am coding a search engine.Basically, if a certain word occurs I need the word immediately after that word to be grabbed and removed.

If the word 'yoga' occurs, I need to remove the word right after it, here 'mats'.So I would get:

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

Ive looked at strpos, but need it for a word. I also have preg_split it to remove words by name, but I additionally need to remove this specific word by position.

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

How would I remove the word after 'yoga', given that the word after it is not always mats. And I still need the words a lot to be there.

Shub
  • 2,686
  • 17
  • 26
Tim
  • 63
  • 1
  • 1
  • 10

3 Answers3

6

This code snippet should do what you are looking for:

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

The code is pretty self-explanatory: separate the sentence in words, identify all keys that have the value 'yoga', unset the next word, and recompose the sentence from the remaining words.

  • This worked. I added in $deleted=$words[$key+1]; right before unset to keep the word as well. Cheers! – Tim Oct 30 '14 at 06:32
1
$sentence = "I like yoga mats a lot.";
$word = "yoga";

echo preg_replace('#(\b' . preg_quote($word) . '\b)\W*\b\w+\b#U', '$1', $sentence);

But the next "word" can be 'a', 'the' and so on. In order to skip those as not the "words" the list should be created and additional manipulations added.

ps: ok, explanation of 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

and what was matching was replace by the content of the capture group $1

Cheery
  • 16,063
  • 42
  • 57
  • This seems to work but can you explain what you did so I can manipulate it as needed. Im not getting how you isolated the word. Also, before I get rid of the word I need to set it equal to a variable, like $deleted_word=$yoga; – Tim Oct 30 '14 at 06:27
  • @Tim I explained the regexp, but not going to show how to get the word as you already decided that other answer solved your problem – Cheery Oct 30 '14 at 06:35
  • The boundaries `\b` around `\w` in `\b\w+\b` are superfluous. Boundary is useful around a literal word but not around character class. – Toto Oct 30 '14 at 08:07
0
<?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;

?>
CS GO
  • 914
  • 6
  • 20
  • This only works if the words yoga and mats are always the same, or at least the same length. They aren't. It could be mats but it could also be water, exercise, tea, etc... – Tim Oct 30 '14 at 06:21