How to find if one string contains another string in PHP?

Search for a command to run...

No comments yet. Be the first to comment.
After a few days of using the 512GB MacBook Neo with TouchID, here are my impressions after some heavy, real-world use. I'll disclaim this review by saying that I consider myself a power user; on my m

2024 was a strange year for me that was a mix of trying new things, accomplishing the best work of my career, and stark transitions into unexplored territory. I think it's important to take a moment every once in a while to step back and look at one'...

What I've Been Up To Four Months Into Freelancing

Leaving My Job with No Plan — My New Career as a Freelancer

With a few glasses of wine in me and some nostalgia-fueled synthwave playing, I found myself reflecting on my past, and my history with computers. I got to thinking of my childhood, and more specifically, my personal history with computers. I love te...

In PHP 8.0 a handy new function was added that allows you to determine if one string contains another substring. This is the str_contains function.
It is used like this:
<?php
str_contains('Hello World', 'Hello');
Returns true. If the string is not found, this returns false. Nice and simple!
If you’re using an older version of PHP, you may want to do something like this.
<?php
strpos('Hello World', 'Hello') !== false;
strpos returns the position in which the substring exists in the string (the very beginning being position 0, so a 0 return also means that the substring was found). This is less direct and readable than the new str_contains function but will work on much older versions of PHP.
strpos vs stripos?strpos is case-sensitive so the capitalization must match. There is also a stripos function that is case insensitive so capitalization doesn’t matter.
<?php
stripos('Hello World', 'hello') !== false;
If you found this article useful, consider following me on Twitter so you don't miss out on more PHP Pro Tips!