preg_match()
The preg_match() function in PHP is used for performing regular expression matches. It is part of PHP's PCRE (Perl Compatible Regular Expressions) functions, which are a set of functions that implement regular expression pattern matching using a syntax that is mostly compatible with Perl's regular expressions.
The syntax is:
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
- Parameters:
$pattern
: The regular expression pattern.$subject
: The input string.$matches
(optional): An array that is filled with the search results.$flags
(optional): Flags to control the match.$offset
(optional): An alternative starting point within$subject
.
- Return Value:
preg_match
returns1
if the pattern matches,0
if it does not, orFALSE
if an error occurs.
An example may be:
if (preg_match("/^example$/", "example")) {
// The pattern matches the string "example"
}
Regular expressions, and by extension preg_match(), are often used in input validation. Proper input validation is a key aspect of securing applications against various forms of injection attacks, including SQL injection (SQLi).
However, preg_match() by itself does not prevent SQLi. It can be part of a broader strategy for ensuring that inputs conform to expected formats (like checking if an input is a valid email address, phone number, etc.), which indirectly reduces the risk of injection attacks.