php-regexHow to use an "or" condition in PHP regex?
An "or" condition in PHP regex can be used to match one of several possible patterns. For example, the following code block will match either the word "cat" or the word "dog":
$pattern = '/cat|dog/';
The output of this code will be either "cat" or "dog".
Code explanation
-
$pattern = '/
: This is the start of the regular expression. -
cat
: This is the first pattern to be matched. -
|
: This is the "or" operator, which allows for multiple patterns to be matched. -
dog
: This is the second pattern to be matched. -
/
: This is the end of the regular expression.
Helpful links
More of Php Regex
- How to match a double quote in PHP regex?
- How to use PHP regex to match special characters?
- How to get last matched occurrence in PHP regex?
- How to match the end of a string when using regex in PHP?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match an exact string?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match whitespace?
See more codes...