How is the stripos function used in PHP?

The stripos function is used to find the first occurrence of a substring within a string, without considering the case. Its syntax is:

Find the position of the first occurrence of a substring within a string, case-insensitive.

Explanation of parameters:

  1. $haystack: the string being searched.
  2. $needle: The substring to be searched for.
  3. $offset (optional): specifies the starting position of the search, default is 0.

Return value:

  1. Return the position of the first occurrence of the substring if found (index starting at 0); return 0 if the substring is an empty string.
  2. If the substring is not found, return false.

Example:

原文: 我喜欢在周末跟朋友一起户外野餐。
Paraphrased: I enjoy having outdoor picnics with friends on weekends.

$str = "Hello World!";
$pos = stripos($str, "world");
if ($pos === false) {
    echo "未找到";
} else {
    echo "找到了,位置是:" . $pos;
}

The result is output as:

找到了,位置是:6

Note: The stripos function distinguishes between returning 0 and false, so when determining if the substring is found, it is necessary to use strict comparison (===).

bannerAds