Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String PHP从字符串中删除一个单词(不是字符)_String_Replace_Str Replace_Words - Fatal编程技术网

String PHP从字符串中删除一个单词(不是字符)

String PHP从字符串中删除一个单词(不是字符),string,replace,str-replace,words,String,Replace,Str Replace,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 A

下面代码的问题是,它从字符串中删除了字符,而不是单词

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

我需要输出为:分钟,从酒窖中取出所有瓶塞

我假设我不能使用
str\u replace()
。我能做些什么来实现这一点

.

将完成以下工作:

$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";
输出:

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

The game start minute, remove all corks from these bottles cellar
该模式如下所示:
/\h+(?:of或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
将完成以下工作:

$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";
输出:

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

The game start minute, remove all corks from these bottles cellar
该模式如下所示:
/\h+(?:of或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

这适用于这个特定的字符串,但是如果需要计算一个新的字符串,并且该字符串以“the”开头,那么该单词将不会被删除。但这很有帮助。更新答案以涵盖字符串中的第一个单词这适用于此特定字符串,但如果需要计算新字符串,且字符串以“the”开头,则不会删除该单词。但这很有帮助。更新了答案以涵盖字符串中的第一个单词