Find substring using strpos() function in PHP

WBOY
Release: 2023-06-27 11:46:01
Original
984 people have browsed it

The strpos() function in PHP is a function used to find whether a string contains another string. This function can be useful in many scenarios, such as verifying whether user input contains sensitive words, or querying keywords in specific text, etc.

In this article, we will introduce in detail how to use the strpos() function in PHP to find substrings.

  1. The syntax of strpos() function

First, let’s take a look at the syntax of strpos() function:

int strpos( string $haystack, mixed $needle [, int $offset = 0 ] )
Copy after login

Among them:

  • $haystack: The string that needs to be searched.
  • $needle: The substring to be found.
  • $offset: Where to start searching. Optional parameter, default is 0.

The return value of this function indicates the position of the first occurrence of the substring in the string. If it is not found, it returns false.

  1. Example Demonstration

Next, let’s look at a few examples to demonstrate how to use the strpos() function.

First, let's find whether a string contains a certain substring. For example: Find whether the string "abcdefg" contains "de".

$str = 'abcdefg';
$pos = strpos($str, 'de');
if ($pos !== false) {
    echo '找到了,位置为:' . $pos;
} else {
    echo '未找到';
}
Copy after login

The above code will output "Found, position: 3" because the substring "de" appears at position 3 in the original string.

Next, let’s find out whether a string contains multiple substrings in another string. For example: Find whether the string "abcdefg" contains "de" and "fg".

$str = 'abcdefg';
if (strpos($str, 'de') !== false && strpos($str, 'fg') !== false) {
    echo '找到了';
} else {
    echo '未找到';
}
Copy after login

The above code will output "Found" because both "de" and "fg" exist in the string.

Finally, let’s demonstrate how to find a substring starting from a specified position. For example: Find "de" starting from position 4 in the string "abcdefghijk".

$str = 'abcdefghijk';
$pos = strpos($str, 'de', 4);
if ($pos !== false) {
    echo '找到了,位置为:' . $pos;
} else {
    echo '未找到';
}
Copy after login

The above code will output "Found, position: 5", because the first "de" that appears starting from position 4 is at position 5.

  1. Summary

The strpos() function is one of the most practical string search functions in PHP. When using, you need to pay attention to the order of parameters and the type of return value (may be false). Mastering the use of this function, we can handle string-related operations more conveniently in PHP.

The above is the detailed content of Find substring using strpos() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template