简体   繁体   English

替代if(preg_match()和preg_match())

[英]alternative to if(preg_match() and preg_match())


I want to know if we can replace if(preg_match('/boo/', $anything) and preg_match('/poo/', $anything)) 我想知道是否可以替换if(preg_match('/boo/', $anything) and preg_match('/poo/', $anything))
with a regex.. 用正则表达式

$anything = 'I contain both boo and poo!!';

for example.. 例如..

From what I understand of your question, you're looking for a way to check if BOTH 'poo' and 'boo' exist within a string using only one regex. 据我对您的问题的了解,您正在寻找一种仅使用一个正则表达式来检查字符串中是否同时存在“ poo”和“ boo”的方法。 I can't think of a more elegant way than this; 我想不出比这更优雅的方式了。

preg_match('/(boo.*poo)|(poo.*boo)/', $anything);

This is the only way I can think of to ensure both patterns exists within a string disregarding order. 这是我可以确保确保两种模式都存在于字符串中而无视顺序的唯一方法。 Of course, if you knew they were always supposed to be in the same order, that would make it more simple =] 当然,如果您知道它们总是应该处于相同的顺序,那将使其更简单=]

EDIT After reading through the post linked to by MisterJ in his answer, it would seem that a more simple regex could be; 编辑阅读完MisterJ在他的回答中链接的帖子后,似乎可以使用更简单的正则表达式了。

preg_match('/(?=.*boo)(?=.*poo)/', $anything);

通过使用管道:

if(preg_match('/boo|poo/', $anything))

You can use the logical or as mentioned by @sroes: 您可以使用逻辑或@sroes所提到的:

if(preg_match('/(boo)|(poo)/,$anything)) the problem there is that you don't know which one matched. if(preg_match('/(boo)|(poo)/,$anything))问题是您不知道哪个匹配。

In this one, you will match "I contain boo","I contain poo" and "I contain boo and poo". 在这一行中,您将匹配“我包含嘘”,“我包含便便”和“我包含嘘和便便”。 If you want to only match "I contain boo and poo", the problem is really harder to figure out Regular Expressions: Is there an AND operator? 如果只想匹配“我包含boo和poo”,那么实际上很难找出正则表达式的问题:是否有AND运算符? and it seems that you will have to stick with the php test. 看来您将不得不坚持php测试。

You can achieve this by altering your regular expression, as others have pointed out in other answers. 正如其他人在其他答案中指出的那样,您可以通过更改正则表达式来实现。 However, if you want to use an array instead, so you do not have to list a long regex pattern, then use something like this: 但是,如果要改用数组,则不必列出长的正则表达式模式,则可以使用如下代码:

// Default matches to false
$matches = false;

// Set the pattern array
$pattern_array = array('boo','poo');

// Loop through the patterns to match
foreach($pattern_array as $pattern){
    // Test if the string is matched
    if(preg_match('/'.$pattern.'/', $anything)){
        // Set matches to true
        $matches = true;
    }
}

// Proceed if matches is true
if($matches){
    // Do your stuff here
}

Alternatively, if you are only trying to match strings then it would be much more efficient if you were to use strpos like so: 另外,如果您仅尝试匹配字符串,则使用strpos效率会更高:

// Default matches to false
$matches = false;

// Set the strings to match
$strings_to_match = array('boo','poo');

foreach($strings_to_match as $string){
    if(strpos($anything, $string) !== false)){
        // Set matches to true
        $matches = true;
    }
}

Try to avoid regular expressions where possible as they are a lot less efficient! 尽量避免使用正则表达式,因为它们的效率要低得多!

从字面上讲条件

if(preg_match('/[bp]oo.*[bp]oo/', $anything))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM