Javatpoint Logo
Javatpoint Logo

PHP STR_CONTAINS ( )

To check if a String Contains a Substring in PHP

STRING: A set of characters can be called string. For example ' H ' is a character and when we use " H " in " HELLO HARRY " it becomes a string and the character " H " can be treated as an element of string .

A string is not only a single set of characters it contains multiple substrings in it. Also we can conclude that all substrings are a subset of string, but a string can b=never be a subset of a substring.

STR_CONTAINS (): - To check whether a parent string contains a substring, PHP provides us with an inbuilt function called str_contains (), which helps the developer identify the parent string contains a given substring or not.

PHP str_contains () function evaluates the substring is contained in the parent string anywhere. Then there is a substring the function will return a TRUE value else it will respond to a False value

Syntax:

Parameters:

S.NO Parameter Description Optional / mandatory
1 Haystack / mains string The original string that we intended to check within Mandatory
2 Needle / substring The substring that we are required to look through the main string (haystack) mandatory

Example 1:

Output:

PHP STR_CONTAINS ( )

In the above program, we have declared a variable $string with string value and used str_contains() function to find the word "s/tring" and declared the if and else statement on the bases of values returned from str_contains() function

Example 2:

Output:

PHP STR_CONTAINS ( )

Here in this program, we have declared a variable $string with string value and used str_contains() function to find the word "string" and declared the if and else statement on the bases of values returned from the str_contains() function

DISADVANTAGES OF STR _ CONTAINS () FUNCTION

  • One of the major disadvantages of using this function is that it is a case sensitive function and will not look for similar words if they are uppercase or camel.
  • Another disadvantage of this function is that it does not return the position or index of the substring present.

ALTERNATE FUNCTION LIKE STR _ CONTAINS ()

To overcome the disadvantage of str _contains, PHP introduced a new set of functions that not only check whether the given string contains a substring. They also state exactly where the substring is located by providing us with the index of the substring.

  • Stripos
  • Strops
  • Strrpos

STROPS - this function is used to return the index of the searched substring

Syntax

Parameters:

S.NO Parameter Description Optional / mandatory
1 string The original string that we intended to check within Mandatory
2 substring The substring that we are required to look through the main string mandatory
3 Start To specify from where the search of substring begins optional

For example :

Output:

PHP STR_CONTAINS ( )

In the above program, we have declared a variable $input_string with string value and used strpos() function to find the word "string" and declared the if and else statement on the bases of values returned from the strpos() function

STRIPS - This function is used to return the first index of the searched substring

Syntax

Parameters:

S.NO Parameter Description Optional / mandatory
1 string The original string that we intended to check within Mandatory
2 substring The substring that we are required to look through the main string mandatory
3 Start To specify from where the search of substring begins optional

Example of STRIPS Function:

Output:

PHP STR_CONTAINS ( )

Here in the above program, we have declared a variable $string and $search with string value and used stripos() function to find the word "string" and its position and declared the if and else statement on the bases of values returned from stripos() function

STRROPS - this function is used to return the last index of the searched substring

Syntax

Parameters:

S.NO Parameter Description Optional / mandatory
1 string The original string that we intended to check within Mandatory
2 substring The substring that we are required to look through the main string mandatory
3 Start To specify from where the search of substring begins optional

Example of STRROPS Function:

Output:

PHP STR_CONTAINS ( )





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA