代码之家  ›  专栏  ›  技术社区  ›  emma

php preg_替换非字母数字字符并选择连词,然后拆分

  •  -1
  • emma  · 技术社区  · 6 年前

    我想在这个字符串中替换:

    This is my Store, it has an amazing design; its creator says it was losing money and he doesn't want to maintain it
    

    所有非字母数字字符,除了 ' (没有)和所有选择的连词:

    is, it, its, the, this, if, so, and
    

    到目前为止,我已经设法获得了这个结果:

    Array
    (
        [1] => This
        [2] => my
        [3] => Store
        [4] => has
        [5] => an
        [6] => amazing
        [7] => design
        [8] => s
        [9] => creator
        [10] => says
        [11] => was
        [12] => losing
        [13] => money
        [14] => and
        [15] => he
        [16] => doesn
        [17] => t
        [18] => want
        [19] => maintain
    )
    

    代码如下:

    $string = "This is my Store, it has an amazing design; its creator says it was losing money and he doesn't want to maintain it";
    $words = array_filter(preg_split('/\s+/', preg_replace('/\W|\b(it|the|its|is|to)|\b/i', ' ', $string)));
    
    print_r($words);
    

    https://3v4l.org/cLrM4

    但正如你所看到的,它正在取代 it 何时更换 its 它也在取代 在里面 doesn't .

    有人能帮我理解我哪里做错了什么吗?XYX

    另外,我也需要它 不区分大小写 那个 /i 非常滑稽的工作:(

    谢谢您!

    1 回复  |  直到 6 年前
        1
  •  1
  •   trincot Jakube    6 年前

    将正则表达式更改为:

    /\W\B|\b(it|the|its|is|to)\b/i
    

    管道在 |\b 对我来说没有意义,可能是打字错误。附加的 \B 之后 \W 将确保仅当非字母字符后面没有紧跟字母字符时才替换它。这比你要求的限制性要小,但也可以用于其他情况,比如有连字符的单词(如岳母)。

    推荐文章