What steps can be taken to refine the search functionality in PHP to only display results that exactly match the search criteria, such as searching for 'l' and '3' without returning results like 'l23'?

To refine the search functionality in PHP to only display results that exactly match the search criteria, you can use regular expressions to match the exact search term. By using the \b metacharacter in the regular expression pattern, you can ensure that the search term is a whole word and not part of a larger string. This will prevent results like 'l23' from being returned when searching for 'l' or '3'.

$searchTerm = 'l'; // Search term
$data = ['apple', 'banana', 'l23', 'orange', '3']; // Sample data

foreach ($data as $item) {
    if (preg_match('/\b' . $searchTerm . '\b/', $item)) {
        echo $item . "\n";
    }
}