PHP String Position

PHP String Position
••• PHP String Position

Now, we dive into the world of strings and all of the crazy functions PHP String Position. Well, not exactly, but we will actually dive into a few of the really major string functions so that you have an understanding of how to work with strings in PHP.

PHP String Position


You might notice that PHP has a lot of functions and not methods, which means you can’t call something like string.method for these functions. First, we will start off with using the string PHP String Position function. Example, please!

Example

$myString = "The meerkat clan was victorious.";
echo strpos($myString, "meerkat",0);

Result

4

The result you get should be 4, unless I failed you. Upon an examination of this code, we see that we have a string variable that has the contents “The meerkat clan was victorious.” We then echo the result of our strpos function.

Our strpos function takes $myString and searches for our next parameter, “meerkat”, beginning from our starting index of 0. If we count out the string, T is 0, h is 1, e is 2, the space is 3, and we have the word meerkat that starts at a position of 4.

String position can be used this way, but it is often used with some other function like when you watch to get a part of a string when using the substr function.

Sometimes, you might use strpos to have an easy way to check if something is in a string. While not the most ideal function to use, it does the trick.

We know the strpos function returns the index of the match, but what happens when it doesn’t find a match? In that case, it returns -1, so we can create an if statement that has a conditional checking if the strpos function returns something greater than -1.

I should mention a few things before we close this tutorial. The strpos function is case-sensitive, which means if I would have searched for “MEERKAT”, it have returned a negative one, which means it didn’t find a match.

Also, the 0 we have is an optional parameterthat tells our search were to begin in the string. So, if we would have had 10 instead of 0, we wouldn’t have returned anything either. This is because “meerkat” begins on the index of 4.

You can use this argument to get sub strings between certain words. Enough with PHP’s string position. Bring on the next string function!

strpos vs strrpos


Often, we assume that strpos and strrpos are the same. Well, the do the same function, but they approach it two different ways. strpos finds the first occurrence of the matching item in the string and returns its position in the string. strrpos starts from the end and returns the position of the last occurrence in the string.

strpos() in PHP

This function helps us to find the position of the first occurrence of a string in another string. This returns an integer value of the position of the first occurrence of the string. This function is case-sensitive, which means that it treats upper-case and lower-case characters differently.

Syntax:

strpos(original_str, search_str, start_pos)

Parameters used:
Out of the three parameters specified in the syntax, two are mandatory and one is optional. The three parameters are described below:

  1. original_str (mandatory): This parameter refers to the original string in which we need to search the occurrence of required string.
  2. search_str (mandatory):This parameter refers to the string that we need to search.
  3. start_pos (optional): Refers to the position of the string from where the search must begin.

Return Type: This function returns an integer value which represents the index of original_str where the string search_str first occurs.

Example:

<?php
 
// PHP code to search for a specific string's position
// first occurrence using strpos() case-sensitive function
function Search($search, $string){
    $position = strpos($string, $search, 5);  
    if ($position == true){
        return "Found at position: " . $position;
    }
    else{
        return "Not Found";
    }
}
 
// Driver Code
$string = "Welcome to GeeksforGeeks";
$search = "Geeks";
echo Search($search, $string);
?>

Output:

Found at position 11

stripos() in PHP

This function also helps us to find the position of the first occurrence of a string in another string. This returns an integer value of the position of the first occurrence of the string. This function is case-insensitive, which means it treats both upper-case and lower-case characters equally.

This function works similarly as strpos() , the difference is that it is case in-sensitive where as strpos() is case sensitive.

Syntax:

stripos(original_str, search_str, start_pos)

Parameters used:
Out of the three parameters specified in the syntax, two are mandatory and one is optional

  1. original_str (mandatory): This parameter refers to the original string in which we need to search the occurrence of required string.
  2. search_str (mandatory): This parameter refers to the string that we need to find.
  3. start_pos (optional):This parameter refers to the position of the string from where the search must begin.

Return Type: This function returns an integer value which represents the index of original_str where the string search_str first occurs.

Example:

<?php
// PHP code to search for a specific string
// first occurrence using stripos() case-insensitive function
function Search($search, $string){
    $position = stripos($string, $search, 5);  
    if ($position == true){
        return "Found at posiion " . $position;
    }
    else{
        return "Not Found";
    }
}
 
// Driver Code
$string = "Welcome to GeeksforGeeks";
$search = "geeks";
echo Search($search, $string);
?>
Found at position 11