Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use an "if X or X" in a preg_match Statement

I'm looking to make a statement in PHP like this:

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

Each of those above by themselves work just fine, but I can't figure out how to make it so that if either of them is true, then to perform the function I have beneath it.

like image 336
Learning Avatar asked Jan 24 '11 16:01

Learning


1 Answers

Use the | to select one value or the other. You can use it multiple times.

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

EDIT: This post made me hungry.

like image 158
Michael Irigoyen Avatar answered Nov 02 '22 23:11

Michael Irigoyen