CodingBison

This page discusses PHP functions that search a character or a substring inside a given string. We begin with a description of these functions and conclude with usage of these functions.

First, let us provide a list of PHP functions that focus on searching a string.

 /* These function return the index of the found string */
 $substring_index = strpos($var_string, $var_substring);
 $substring_index = strrpos($var_string, $var_substring);

 /* These function return the string where the substring is found */
 $start_of_substring = strstr($var_string, $var_substring);
 $start_of_substring = strchr($var_string, $var_substring);
 $start_of_substring = strrchr($var_string, $var_character);
 $start_of_substring = stristr($var_string, $var_substring);

The first two functions (strpos() and strrpos()) in the above list search for a character or a substring in a given string. For both of these functions, the first argument is the main string and the second argument is the substring being searched. When found, these functions return the index of the character in the given string; for a substring, these functions return the index that marks the beginning of the substring in the given string.

strpos() returns the index of the first occurrence, while searching in the forward direction and with the search starting at the start of the string. strrpos() returns the index of the first occurrence, while searching in the reverse direction and with the search starting at the end of the string and searching in the reverse direction.

The remaining four functions in the above list do not return the index of the substring. Instead, they return part of the main string ($var_string) starting at the first occurrence of the substring ($var_substring) being searched. If the character or the substring is not found, then these functions return NULL.

strstr() locates the first occurrence of a substring. Similarly, strchr() returns the first occurrence of a character in a given string; in fact, strchr() is merely an alias of strstr(). strrchr() returns the first occurrence of a given character, but from the reverse direction. Please note that there is no strrstr() function and hence strrchr() is not an alias! Lastly, if the case does not matter for search, then we can use stristr(), which is a case insensitive variant of strstr().

Next, we provide two simple examples that show the usage for above functions. The first function focuses on strpos() and strrpos() functions. We provide below the example along with its output.

 <?php
 $var_string = "A useful social network should have a lot of users";

 $var_position = strpos($var_string, "use");
 echo "[strpos] The position for the string 'use' is $var_position <br>";

 $var_position = strrpos($var_string, "use");
 echo "[strrpos] The position for the string 'use' is $var_position <br>";

As expected, the output shows that if a string has multiple occurrences of a substring (e.g. the substring "use" in the following examples), then strpos() returns the first occurrence from the start and strrpos() returns the first occurrence from the end.

 [strpos] The position for the string 'use' is 2
 [strrpos] The position for the string 'use' is 45

The second example focuses on the remaining functions: strstr(), strchr(), strrchr(), and stristr().

 <?php

 $var_string = "A useful social network should have a lot of users";

 /* Searching for a substring/character using strstr() */
 $var_position = strstr($var_string, "use");
 echo "[strstr] The returned string for substring 'use' is: $var_position <br>";

 $var_position = strstr($var_string, "s");
 echo "[strstr] The returned string for character 's' is: $var_position <br>";

 /* strchr() is an alias for strstr() */
 $var_position = strchr($var_string, "use");
 echo "[strchr] The returned string for substring 'use' is: $var_position <br>";

 $var_position = strchr($var_string, "s");
 echo "[strchr] The returned string for character 's' is: $var_position <br>";

 /* strrchr() searches from reverse and unlike strchr(), is not an alias */
 $var_position = strrchr($var_string, "s");
 echo "[strrchr] The returned string for character 's' is: $var_position <br>";

 /* stristr() is the case insensitive variant of an alias for strstr() */
 $var_position = stristr($var_string, "uSE");
 echo "[stristr] The returned string for substring 'use' is: $var_position <br>";

 $var_position = stristr($var_string, "S");
 echo "[stristr] The returned string for character 's' is: $var_position <br>";
 ?>

We provide the output below. The output confirms that strchr() is in fact merely an alias for strstr().

 [strstr] The returned string for substring 'use' is: useful social network should have a lot of users
 [strstr] The returned string for character 's' is: seful social network should have a lot of users
 [strchr] The returned string for substring 'use' is: useful social network should have a lot of users
 [strchr] The returned string for character 's' is: seful social network should have a lot of users
 [strrchr] The returned string for character 's' is: s
 [stristr] The returned string for substring 'use' is: useful social network should have a lot of users
 [stristr] The returned string for character 's' is: seful social network should have a lot of users




comments powered by Disqus