How to use "if X or X" in preg_match prescription

I want to make an expression in PHP as follows:

if(preg_match("/apples/", $ref1, $matches)) OR if(preg_match("/oranges/", $ref1, $matches)) { Then do something }

Each of them works fine on its own, but I can’t figure out how to do it in such a way that if any of them is right, then perform the function that I have below.

+3
source share
3 answers

Simple, use the logical OR operator:

if (expression || expression) { code }

For example, in your case:

if(
    preg_match("/qualifier 1/", $ref1, $matches) ||
    preg_match("/qualifier 2/", $ref1, $matches)
) {
    do_something();
}
+2
source

Use |to select one value or another. You can use it several times.

preg_match("/(apples|oranges|bananas)/", $ref1, $matches)

EDIT: This post made me hungry.

+14
source

$matches:

preg_match('/(apples|oranges)/', $ref1, $matches)

, $matches ( , , ):

preg_match('/(?:apples|oranges)/', $ref1, $matches)
+7

Source: https://habr.com/ru/post/1787492/


All Articles