╰┈➤The preg_match() function returns whether a match was found in a string, preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or false on failure (error).
$string = "hello world";
$pattern = "/world/";
echo preg_match($pattern, $string);
// outputs 1
► As usual, we use this function together with Regex (Regular Expression) syntax and we adapt it to the different solutions. for example, in regular expression "i" means case insensitive check. ◄
therefore if that case would be like that:
$string = "Hello wORLD";
$pattern = "/world/i";
echo preg_match($pattern, $string);
// it would still outputs 1
► Valeri has used this function in a task to check for the only uppercase letters, only lowercase letters and first uppercase letter in the string via regex. ◄
For example: (via Regex - Regular Expression and preg_match)
(^[A-Z]*$) it means if there are only uppercase letters in a string.
(^[a-z]+$) it means if there are only lowercase letters in a string.
(^[A-Z]{1}[a-z]*$) it means if the first letter is uppercase in a string.
| - it means "Logical or" via regex as well.
by Luka Khitaridze
2 years ago
PHP
Functions
0
Pro tip: use ```triple backticks around text``` to write in code fences