Php string contains one of several words

A lot of times when I am working with strings in PHP, it becomes important to check if a string contains another substring. PHP has a lot of functions to help you manipulate strings any way you like. We will be using some of these functions today to learn how to check if a string contains a specific substring.

Using New Functions in PHP 8

Three new functions have been added in PHP 8 to help us figure out if a string contains another substring. Please keep in mind that all these functions are case sensitive, and checking for the existence of an empty substring with them will always return true.

  1. str_contains[] checks if a string contains a specific substring.
  2. str_ends_with[] checks if a string ends with a specific substring.
  3. str_starts_with[] checks if a string starts with a specific substring.

As I mentioned earlier, these functions perform a case-sensitive match. You can also perform a case-insensitive match with them by first converting both the string and substring to the same case using strtolower[] or strtoupper[].

These functions are ideal if you don't want to get any extra information about the substrings, like their position. People who need extra information about the match should consider using the functions below.

Using strpos[] and stripos[]

You can use the PHP strpos[] function to get the position of the first occurrence of a substring in a string. The function will return false if it cannot find the substring.

When you use strpos[] to check for a substring, make sure that you use the strict inequality operator. This is because the position returned by strpos[] starts with 0, and 0 can also equate to false. Using a regular equality check will give you false negatives if the substring is right at the beginning of the main string.

Here are some examples of strpos[]:

As you can see in the above example, Angela is not at the beginning of our sentence, so it would have a non-zero position which evaluates to true.

On the other hand, Amy is right at the beginning, and its position of 0 evaluates to false, even though it exists in the string. Therefore, always make sure that you evaluate the value of strpos[] with !== because there is no way to know where a substring might occur when dealing with strings in real life.

Sometimes, you might need to make this check of a substring in a string case-insensitive. In that case, you can simply use the stripos[] function in PHP. It works exactly like strpos[] but makes the search case-insensitive.

Using strstr[] and stristr[]

By default, the strstr[] function returns a portion of the main string, starting from the substring and going to the end of the main string. It will return false if the substring does not exist inside the main string.

We can use this information to check the existence of a substring inside a string. All we have to do is evaluate the value returned by strstr[] and check if it is false or not. Just like last time, we will be using the strict inequality operator !== to do the check.

Here are some examples:

You can use stristr[] in place of strstr[] to make the search case-insensitive. All other behavior of stristr[] stays the same.

Final Thoughts

In this quick tip, we went over three different ways of checking if a string contains a substring in PHP. You can use any of these functions depending on what information you need from the string. Using the new PHP 8 functions will allow you to skip any type of strict checking, but you will need to use the older functions if you want to know the position of the substring or get a part of the main string as the return value.

Did you find this post useful?

Freelancer, Instructor

I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around.

How do I check if a string contains multiple words in PHP?

You can use the PHP strpos[] function to check whether a string contains a specific word or not. The strpos[] function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

Does string contain PHP?

The str_contains is a new function that was introduced in PHP 8. This method is used to check if a PHP string contains a substring. The function checks the string and returns a boolean true in case it exists and false otherwise. However, keep in mind that str_contains is case-sensitive.

How will you locate a string within a string in PHP?

The strpos[] function finds the position of the first occurrence of a string inside another string. Note: The strpos[] function is case-sensitive.

How do you check whether a string contains any words from an array?

To check if a string contains a substring from an array:.
Use the Array. some[] method to iterate over the array..
Check if the string contains each substring..
If the condition is met, the string contains a substring from the array..

Bài mới nhất

Chủ Đề