Home > Backend Development > PHP Tutorial > Detailed explanation of PHP's methods and functions similar to indexof for processing strings

Detailed explanation of PHP's methods and functions similar to indexof for processing strings

不言
Release: 2023-03-29 14:46:02
Original
1385 people have browsed it

This article mainly introduces the relevant information on the detailed explanation of PHP's indexof-like method and function for processing strings. Friends in need can refer to the following

Detailed explanation of PHP's indexof-like method and function for processing strings

There are two functions or methods similar to indexof for processing strings in PHP. They are the strpos function and the stripos function. The usage of these two functions is similar.

When the strpos function processes a string, if it contains the string, it returns the position of the first occurrence of the string, and returns false if it does not appear. Sensitive to string case.

When the stripos function processes a string, if it contains the string, it returns the position of the first occurrence of the string, and returns false if it does not appear. String case insensitivity.

PHP strpos() function

Definition and usage

strpos() Function finds the first occurrence of a string within another string.

Note: The strpos() function is case-sensitive.

Note: This function is binary safe.

Syntax

strpos(string, find, start)
Copy after login

Parameter description

string : Required. Specifies the string to search for.
find: required. Specifies the string to search for.
start: optional. Specifies the location from which to start the search.

Return value

Returns the position of the first occurrence of a string in another string, or returns FALSE if the string is not found .

Note: The string position starts from 0, not from 1.

Example

<?php
echo strpos(&#39;Hello world!&#39;, &#39;wo&#39;);
?>
Copy after login

Output: 6

PHP stripos( ) Function

#Definition and usage

stripos() function finds the first occurrence of a string in another string Location.

Note: The stripos() function is not case-sensitive.

Note: This function is binary safe.

Syntax

stripos(string,find,start)
Copy after login

Parameter description

string : Required. Specifies the string to search for.
find: required. Specifies the string to search for.
start: optional. Specifies the location from which to start the search.

Return value

Returns the position of the first occurrence of a string in another string, or returns FALSE if the string is not found .

Note: The string position starts from 0, not from 1.

Example

<?php
echo stripos(&#39;Hello world!&#39;, &#39;WO&#39;);
?>
Copy after login

Output: 6

Thanks for reading, I hope it can help everyone, Thank you for your support to the PHP Chinese website!

Related recommendations:

Detailed explanation of conversion between arrays and XML processed by PHP

The above is the detailed content of Detailed explanation of PHP's methods and functions similar to indexof for processing strings. 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