How can regular expressions be used in PHP to search for specific patterns in database entries?

Regular expressions can be used in PHP to search for specific patterns in database entries by using functions like preg_match() or preg_match_all(). These functions allow you to define a regular expression pattern to search for within a given string. You can then use this functionality to search for specific patterns within database entries, such as email addresses, phone numbers, or other specific formats.

// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');

// Query to select all entries from a specific table
$query = "SELECT * FROM table_name";
$result = $connection->query($query);

// Loop through each entry and search for a specific pattern using regular expressions
while ($row = $result->fetch_assoc()) {
    if (preg_match('/[0-9]{3}-[0-9]{3}-[0-9]{4}/', $row['phone_number'])) {
        echo "Phone number found: " . $row['phone_number'] . "<br>";
    }
}

// Close the database connection
$connection->close();