What is the purpose of the preg_match function in PHP and how does it work?
The preg_match function in PHP is used to perform a regular expression match on a string. It is commonly used to check if a specific pattern exists in a given string. The function returns 1 if the pattern is found in the string, and 0 if not found.
<?php
$string = "Hello, World!";
$pattern = "/Hello/";
if (preg_match($pattern, $string)) {
echo "Pattern found in the string.";
} else {
echo "Pattern not found in the string.";
}
?>
Related Questions
- How can changing the data type of a column in a MySQL table affect the results of a query in PHP?
- How can error handling be improved in the provided PHP script to prevent unexpected behaviors?
- What are the potential pitfalls of using inheritance to extend a database class for additional functionality in PHP?