DEV Community

Cover image for Review of new string functions in PHP 8.1
Dollar Dev
Dollar Dev

Posted on

Review of new string functions in PHP 8.1

Hi,
I decided to start my first post specifically with the new PHP string functions. I work with many lines of code every day and thanks to these functions the code will be more pleasant.
Please support this post with reactions and I will continue to please the DEV community with new posts about the PHP backend.
I'll start, perhaps, with how it was in previous versions of PHP and what were the options for checking for the content of a substring in a string - str_contains().

Using strstr():

$fullstr = "It is a nice day today.";
$needle = "nice";

if (strstr($fullstr , $needle) !== false) {
    echo "The fullstr contains the needle.";
} else {
    echo "The fullstr does not contain the needle.";
}
Enter fullscreen mode Exit fullscreen mode

The strstr() function returns the portion of the string starting from the first occurrence of the needle to the end of the string. If the needle is not found, it returns false.

Using preg_match():

$fullstr = "It is a nice day today.";
$needle = "/nice/";

if (preg_match($needle, $fullstr)) {
    echo "The fullstr contains the needle.";
} else {
    echo "The fullstr does not contain the needle.";
}
Enter fullscreen mode Exit fullscreen mode

In this example, preg_match() is using a regular expression to match the needle in the fullstr. The preg_match() function returns 1 if the pattern is found, 0 if no match is found, and false on error.
While these alternatives are available, the choice between them depends on the specific requirements and patterns you are working with. It's essential to consider factors like performance, flexibility, and the nature of the data you're handling.

Using strpos():

$fullstr = "It is a nice day today.";
$needle = "nice";

if (strpos($fullstr , $needle) !== false) {
    echo "The fullstr contains the needle.";
} else {
    echo "The fullstr does not contain the needle.";
}
Enter fullscreen mode Exit fullscreen mode

In PHP 7.4, you may also use the null coalescing assignment operator (??=) to provide a default value in case the substring is not found. Here's an example:

$fullstr = "It is a nice day today.";
$needle = "nice";

$result = strpos($fullstr , $needle) ?? -1;

if ($result !== -1) {
    echo "The fullstr contains the needle.";
} else {
    echo "The fullstr does not contain the needle.";
}
Enter fullscreen mode Exit fullscreen mode

In this example, if strpos() returns false (needle not found), the null coalescing operator sets the value of $result to -1. You can then check against this value to determine if the needle is present.
Remember that PHP 7.4 and later versions offer a range of improvements and new features, so it's generally recommended to use the latest stable version for security and performance reasons.

And finally using str_contains():

$fullstr = "It is a nice day today.";
$needle= "nice";

if (str_contains($fullstr , $needle)) {
    echo "The fullstr contains the needle.";
} else {
    echo "The fullstr does not contain the needle.";
}
Enter fullscreen mode Exit fullscreen mode

The str_contains() function in PHP is used to check if a string contains another substring. It returns true if the substring is found, and false otherwise.

Personally, I like the last string search option the most :)
Thank you for your attention and I hope this material will be useful.

Top comments (0)