3 Ways To Check If a PHP String Contains Another

Probably the most frustrating part of writing PHP code figuring out if a PHP String Contains another string. But not to worry – in this tutorial, we’re going to show how easy it is to identify a substring using several methods. Apart from showcasing them, we’re also going to the discuss their pros and, of course, their cons. Please keep in mind that there’s more than one method of ‘rooting’ out substrings.




A couple of Considerations If PHP String Contains Another

The first major mistake in testing for a substring is using the wrong comparison operator. For instance, if you’re using != to check for a substring, you could end up with what it’s called a false positive. In this case, it’s better to use !==, which compares the type.

If you’re having trouble figuring out which operator to use in a certain context, you can refer to this PHP tutorial for detailed information on operators and their functions. You will also find code examples explained for the novice programmer.

As we’ve said, there are different methods for figuring out if a string contains a word or another string. To the date, there are five methods, but only the first three of them are quick.

  1. stripos() for case sensitive to check out if a string contains another string.
  2. Regex for string in string
  3. strstr() for a string in another string.

1.Checking for a substring using the case-sensitive string reference stripos()

The quickest and painless way to check if a string contains another one is by employing the stripos() function which basically searches if a string contains a particular word. What happens is that the function will return TRUE if the string contains a substring/word or FALSE if the string contains neither of them.




To help you visualize how stripos() works, here’s an example.

$first_string = “I am 10 years older than you.”

$sought_word= “years”

$character = “I”




$targeted_substring = “10 years”

If (strpos ($first_String, $sought_word) !== false {

                echo ‘This word “ ‘.$sought_word.’” is in the string.’;

}

(this will tell you that the world “years” can be found in this particular string)




if (stripos ($first_string, $character) !== false)  {

echo ‘This character “ ‘.$character.’ “can be found in this particular string.’;

}

(these lines will tell you that “I” can be found in the given string)

if (strpos ($first_string, $targeted substring) !== false {




echo ‘The targeted substring “ ‘.targeted_substring.’”can be found in this string.”

(these lines will tell you that the substring “5 years” can be found in the string).

if (strpos($first_string, $character) != false) {

echo ‘This character “ ‘.$character.’ “shows up in this string.”

} else {

echo ‘This character “ ‘.$character.’ “ doesn’t show up in this string.

(These lines will tell you that the character “I” can’t be found in that particular string.)

2.Regex for a substring in a string

Regular expressions aka Regex are better suited for searching complex patterns within a given substring.

Here’s an example of how to search for substrings, using the functions defined in the previous example.

$first_string = “I am 10 years older than you.”

$sought_word= “years”

$character = “I”

$targeted_substring = “10 years”

If (preg_match(‘/years/’, $first_string)) {

echo ‘The word “ ‘.$sought_word.’ “can be found in this string.’;

}

(tells the user that the word “years” can be found in that string)

if (preg_match(‘/I/’, $first_string)) {

echo ‘The character “’.$character.; “shows up in this string.’ ;

}

(tells you that “I” shows up in your string.)

If (preg_match(‘/10 years/’, $first_string)) {

echo ‘This substring : ‘.$targeted_substring.’ “shows up in the provided string.’

}

(tells you that the substring, defined as “10 years” shows up in your declared string).

3. Strstr() functions to find substrings in strings

The strstr () function is quite versatile, meaning that it can be used to search for substrings, characters, and words within a specific string. What it does is basically returning only the part that contains the first string, from the first occurrence of the substring/word all the way back to the end. Here’s an example to help you figure out how the function

works.

$first_string = “I am 10 years older than you.”

$sought_word= “years”

$character = “I”

$targeted_substring = “10 years”

If (strstr($first_string, $sought_word) !==false) {

echo ‘The word “ ‘.$sought_word.’”shows up in the string.;

}

(tells you that “years” pops up in the string)

if (strstr($first_string, $character) !==false) {

echo ‘The character” ‘$character.’” shows up in the given string.’;

}

(tell the user that the character “I” shows up in the string)

if (strstr($first_string, $targeted_substring) !==false) {

echo ‘The substring “’.$targeted_substring.; “ shows up in the string.’;

}

Conclusion

There you have it, folks – three simple ways of figuring out if the string you’re testing has a substring or not. Again, be careful around operators, and be sure to test out your string using more than one method to avoid false-positive results.

If you want to learn PHP or any language, I suggest taking the online course from any one of the top online learning platforms like Udemy which gives you any type of course for very less price. You can read complete details about Udemy. You can also check our Udemy discount coupon page for the latest offers.

If you like this tutorial about methods to check PHP string contains another string, please share it. Want more tips, Please follow whatvwant on Facebook, Twitter, and YouTube.

Comments Rating 0 (0 reviews)



Leave a Reply

Sending