Coding the AND Operator in PHP Regular Expression
Advanced PHP Regular Expressions - Part 8
Foreword: In this part of the series, I explain how to code the AND operation for a PHP regular expression.
By: Chrysanthus Date Published: 11 Jul 2019
Introduction
Motivation
In this section I explain what motivated me to produce this article. As of today, if you want to match this or that in a subject string, you have to produce a regex like:
/this|that/
where | is the regex OR operator. Is there an equivalent operator for AND? - No. If you want it, you have to code it. In this article I give you a simple solution that works with all systems.
The Solution
Above, you have two alternatives (this or that). You can actually have more than two alternatives. AND means all alternatives must be present in the subject string. The solution is surprisingly simple: Just make sure each of the alternatives is found in the subject, with a for-loop.
Algorithm
- Place all the alternatives in an array.
- Verify if each alternative is in the subject with regular expression technique in the for-loop.
- Any alternative not in the subject means false for the overall operation.
If all alternatives are in the subject, that means true, for the overall operation. So, that is my solution on how to code the AND operator for PHP regex, until PHP gives us an operator for AND.
Code Example
The following code illustrates this with two code segments. The first segment returns TRUE and the second one returns FALSE. The alternatives are: “pencil”, “book” and “pen”. Read and test the code:
<?php
$subject = "This is a pencil. Is that a book? I use a pen.";
$arr = Array("pencil", "book", "pen");
$booll = 'false';
for ($i=0;$i<count($arr);++$i)
{
$regex = '/' . $arr[$i] . '/';
if (preg_match($regex, $subject) === 1)
{
$booll = 'true';
}
else
{
$booll = 'false';
break;
}
}
echo $booll, '<br>';
$subject = "This is a pencil. I use a pen.";
$arr = Array("pencil", "book", "pen");
$booll = 'false';
for ($i=0;$i<count($arr);++$i)
{
$regex = '/' . $arr[$i] . '/';
if (preg_match($regex, $subject) === 1)
{
$booll = 'true';
}
else
{
$booll = 'false';
break;
}
}
echo $booll;
?>
The output is:
true
false
Each of the code segments simulates the AND operator. In your own project, you can vary the AND code, but the algorithm stays the same.
That is it for this part of the series.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links