0

The problem with the code, below, is that it removes characters from the string rather than the words.

<?php
    $str = "In a minute, remove all of the corks from these bottles in the cellar";

    $useless_words = array("the", "of", "or", "in", "a");

    $newstr = str_replace($useless_words, "", $str);

   //OUTPUT OF ABOVE:  "In mute, remove ll   cks from se bottles   cellr"
?>

I need the output to be: minute, remove all corks from these bottles cellar

I'm assuming I can't use str_replace(). What can I do to achieve this?

.

bagofmilk
  • 1,492
  • 8
  • 34
  • 69

2 Answers2

1
$useless_words = array(" the ", " of ", " or ", " in ", " a ");
$str = "In a minute, remove all of the corks from these bottles in the 
cellar";

$newstr = str_replace($useless_words, " ", $str);

$trimmed_useless_words = array_map('trim',$useless_words);
$newstr2 = '';
foreach ($trimmed_useless_words as &$value) {
   if (strcmp($value, substr($newstr,0,strlen($value)))){
       $newstr2 = substr($newstr, strlen($value) );
       break;
   }
}
if ($newstr2 == ''){
    $newstr2 = $newstr; 
}
echo $newstr2;
Mike M
  • 488
  • 5
  • 19
  • This works for this particular string, but if a new string needs to be evaluated and the string starts with "the", then that word wouldn't be removed. But this is helpful. – bagofmilk Mar 16 '18 at 19:00
  • Updated answer to cover the first word in the string – Mike M Mar 16 '18 at 19:46
1

preg_replace will do the job:

$str = "The game start in a minute, remove all of the corks from these bottles in the cellar";
$useless_words = array("the", "of", "or", "in", "a");
$pattern = '/\h+(?:' . implode($useless_words, '|') . ')\b/i';
$newstr = preg_replace($pattern, "", $str);
echo $newstr,"\n";

Output:

The game start minute, remove all corks from these bottles cellar

Explanation:

The pattern looks like : /\h+(?:the|of|or|in|a)\b/i

/                   : regex delimiter
  \h+               : 1 or more horizontal spaces
  (?:               : start non capture group
    the|of|or|in|a  : alternatives for all the useless words
  )                 : end group
  \b                : word boundary, make sure we don't have a word character before
/i                  : regex delimiter, case insensitive
Toto
  • 89,455
  • 62
  • 89
  • 125