In PHP, how do I search strings with PHP 8+ features?

In PHP 8 and above, you can use various functions to search through strings effectively. For instance, using the new str_contains, str_starts_with, and str_ends_with functions makes it easier to check for substrings.

<?php $haystack = "Hello, welcome to PHP 8 string manipulation!"; // Check if a substring exists if (str_contains($haystack, "welcome")) { echo "The substring 'welcome' was found!"; } // Check if a string starts with a specific substring if (str_starts_with($haystack, "Hello")) { echo "The string starts with 'Hello'"; } // Check if a string ends with a specific substring if (str_ends_with($haystack, "manipulation!")) { echo "The string ends with 'manipulation!'"; } ?>

PHP 8 string search str_contains str_starts_with str_ends_with