Check word exist in string PHP?

In this tutorial explained of check word exist in string php. I need to find word from string, I mean check string contains word exist or not but i try to do, a last i found two step we can check easily

We have explained of if a word exists in a string in PHP, you can consider the following step based on the provided sources:

  1. strpos(): A PHP function used to find the position of the first occurrence of a substring within a string. used to check if a word exists within a string.
  2. preg_match(): PHP function matching pattern within string using regular expressions. It can be used to check if a specific word in string.
1. Check word exist in string php using preg_match() function

To check if a PHP string contains a specific string using preg_match(), you can create a regular expression pattern and utilize the preg_match() function.

<?php

    $new_str = "Hi i hope you are doing well";

    $check_string = preg_match("doing", $new_str);
    
    if($check_string == 1) {
        print_r("Found word");
    }
?>
2. Check word exist in string php using substr() function
<?php

    $new_str = "Hi i hope you are doing well";

    if(strpos($new_str, "doing") !== false) {
        print_r("Word found");
    }

?>
3. Check word exist in string php using str_contains() function

To check if a PHP string contains a specific string using the str_contains() function.

<?php

new_str = "Hi i hope you are doing well";

if(str_contains($new_str, "doing")) {
    print_r("Word found");
}

?>

I hope this tutorial help you.

Leave a Reply

Your email address will not be published. Required fields are marked *