gpt4 book ai didi

PHP preg_match "AND"运算符

转载 作者:行者123 更新时间:2023-12-04 14:48:43 31 4
gpt4 key购买 nike

我使用“或”运算符“|”只匹配 $name 变量的单词

$name = "one five six two";
if (preg_match('/(one|two)/i', $name)) {return true;}

如果单词在 $name 内,我应该使用什么运算符与 preg_match 一起使用“AND”条件?

IE。
if (preg_match('/(two "AND" five)/i', $name)) {return true;}

最佳答案

如果您仍然想使用正则表达式,则需要积极的前瞻:

if (preg_match('/^(?=.*one)(?=.*two)/i', $name)) {return true;}

不建议将它用于简单的东西(有点矫枉过正),并且它会因更复杂的东西而变得困惑......

关于PHP preg_match "AND"运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22222574/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com