Home  >  Article  >  Backend Development  >  PHP Function Guide: strpos()

PHP Function Guide: strpos()

WBOY
WBOYOriginal
2023-06-20 08:39:076748browse

PHP is a widely used programming language that is used to create web applications and websites. Its powerful function library makes PHP one of the preferred languages ​​​​for developers. Today, we will focus on a very important function in the PHP function library: strpos().

The function of strpos() function is to find the specified substring in the string and return the position of the first occurrence. If not found, returns false. The syntax of the strpos() function is as follows:

strpos(string $haystack, mixed $needle, int $offset = 0): int|false

In the above syntax:

  • $haystack is the main string to be searched in.
  • $needle is the substring that needs to be found. Can be a string or character.
  • $offset is an optional parameter, which character or string position to start searching at. The default is 0.
  • int|false means that the returned result is an integer (the position of the substring) or false (if not found).

Let's see how strpos() is used, with some examples.

Example 1:

We will use the strpos() function to find a substring embedded in a string.

$string = "There is needle in this haystack.";
$pos = strpos($string, "needle");

if ($pos === false) {
    echo "The string 'needle' was not found in the string!";
} else {
    echo "The string 'needle' was found in the string, starting at position $pos.";
}

In the above code, we first define the string to search for. Then we use the strpos() function to find the first occurrence of the substring "needle". We use the === operator to determine whether the return value is false. If not found, an appropriate message is output. Otherwise, the string "The string 'needle' was found in the string, starting at position $pos." is printed.

Example 2:

You can also use the $offset parameter to specify where to start the search:

$string = "There is a needle in this haystack and another needle in this haystack.";
$needle = "needle";
$occurance = 2;

$pos = strpos($string, $needle);
if (!$pos) {
    echo "String '$needle' not found in '$string'";
} else {
    $j = 1;
    while ($j < $occurance) {
        $pos = strpos($string, $needle, $pos + 1);
        if (!$pos) {
            break;
        }
        $j++;
    }

    if ($pos) {
        echo "The $occurance occurance of '$needle' found at position $pos";
    } else {
        echo "The $occurance occurance of '$needle' not found in '$string'";
    }
}

above In the code, we define the string we want to search for, and then use the variable $needle to store the substring we want to find. We use a variable $occurance to store the time we want to find the substring. We store the position of the first match in $pos and then use a while loop to find other matches. Finally, we print out the results.

Note: strpos()The function is case-sensitive. If you want a case-insensitive search, use the stripos() function.

Summary:

In this article, we describe the basic usage of the strpos() function in PHP and two examples. This function is one of the very useful functions in daily PHP programming. I hope this article will help you quickly master this function.

The above is the detailed content of PHP Function Guide: strpos(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn