首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在进行字符串替换/删除时跳过单词/短语(如果在引号中

在进行字符串替换/删除时跳过单词/短语(如果在引号中
EN

Stack Overflow用户
提问于 2018-06-04 07:25:06
回答 2查看 55关注 0票数 0

假设我有一个这样的字符串

我今年7月从底特律飞往温哥华

代码语言:javascript
复制
$string = 'I am flying from "Detroit to Vancouver" this July';

我还有一个"stopwords“数组(我选择从字符串/字符串中删除的单词)

代码语言:javascript
复制
$stopwords = array( "to", "anotherstopword", "andanother" )

现在我只是在用

代码语言:javascript
复制
$string = str_replace($stopwords, ' ', $string);

这当然给了我string(33) "I am flying from "Detroit Vancouver" this July"

我在想也许可以在str_replace前加一个空格来炸掉$string,这样我就可以

代码语言:javascript
复制
Array
(
    [0] => I
    [1] => am
    [2] => flying
    [3] => from
    [4] => "Detroit
    [5] => to
    [6] => Vancouver"
    [7] => this
    [8] => July
)

然后可能从数组中删除它们,执行替换,然后重新插入它们。但这似乎有点过头了。

我也考虑过使用下面这样的函数

代码语言:javascript
复制
  function getStringBetween($str, $from, $to, $withFromAndTo = false)
  {
      $sub = substr($str, strpos($str, $from) + strlen($from), strlen($str));
      if ($withFromAndTo)
          return $from . substr($sub, 0, strrpos($sub, $to)) . $to;
      else
          return substr($sub, 0, strrpos($sub, $to));
  }

当这样做的时候,

代码语言:javascript
复制
    echo '<pre>';
    print_r(getStringBetween($string, '"', '"'));
    echo '</pre>';

输出:

底特律到温哥华

并在str_replace之前执行某种类型的忽略条件。

但只要字符串中有多个引号,此操作就会失败。

理想情况下,我想创建一个条件,如果字符串包含双引号,则在str_replace过程中完全忽略它们。

当然,我并不反对使用preg_replace这样的str_replace以外的工具,但是我没有足够的经验来为我预期的输出生成样本。

有没有人能想出一个好方法,在进行替换之前忽略要删除的停用词/词?

编辑:

代码示例

代码语言:javascript
复制
<?php

  $stopwordstest = array( " to ", " a ", " test " );

  $string = 'I am flying from "Detroit to Vancouver" this July when the weather is test nice';

  var_dump($string);

// as is, without string replace
// string(79) "I am flying from "Detroit to Vancouver" this July when the weather is test nice" 

  $string = str_replace($stopwordstest, ' ', $string);

  echo '<br><br>';

  var_dump($string);

// string(71) "I am flying from "Detroit Vancouver" this July when the weather is nice"

// Expected output is:
//
// string(74) "I am flying from "Detroit to Vancouver" this July when the weather is nice"
//

?>

换句话说,我希望字符串替换按预期进行,但是由于单词to封装在引号("Detroit to Vancouver")中,因此应该跳过这个单词,因为它在引号中。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-04 08:21:05

使用正则表达式会更容易,使用PHP (PCRE)会更容易。使用PCRE,您可以使用(*SKIP)回溯动词进行匹配和跳过。你匹配一个双引号的字符串,然后让引擎从整体匹配中跳过这一部分,并在交替的第二侧键入你想要的模式。

代码语言:javascript
复制
"[^"\\]*(?:\\.[^"\\]*)*"(*SKIP)(*F)

上面的正则表达式匹配双引号字符串(包括转义的双引号),然后告诉引擎忘记。

这将是实现此功能的PHP代码,以及在正则表达式中收集停用词:

代码语言:javascript
复制
echo preg_replace('/"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"(*SKIP)(*F)|\b(?:'
    . implode('|', array_map('preg_quote', $stopwords))
    . ')\b\h*/', '', $string);

Live demo

票数 1
EN

Stack Overflow用户

发布于 2018-06-04 08:09:41

代码语言:javascript
复制
foreach ($stopwords as &$stopword) {
    $string = str_replace($stopword, ' ', $string);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50671864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档