Home  >  Article  >  Backend Development  >  How to use php stristr function?

How to use php stristr function?

藏色散人
藏色散人Original
2019-05-30 13:31:593644browse

php The stristr function is used to search for the first occurrence of a string in another string. Its syntax is stristr(string,search,before_search). The parameter string is required and specifies the string to be searched; search is required and specifies The string to search for.

How to use php stristr function?

#php How to use stristr function?

Definition and Usage

stristr() function searches for the first occurrence of a string within another string.

Note: This function is binary safe.

Note: This function is not case sensitive. For case-sensitive searches, use the strstr() function.

Syntax

stristr(string,search,before_search)

Parameters

string Required. Specifies the string to be searched for.

search Required. Specifies the string to search for.

If the parameter is a number, search for characters matching the ASCII value corresponding to the number.

before_search Optional. The default value is a Boolean value of "false".

If set to "true", it will return the portion of the string before the first occurrence of the search parameter.

Return value: Returns the remaining part of the string (from the matching point). Returns FALSE if the searched string is not found.

PHP version: 4

Change log:

In PHP 5.3, the before_search parameter is added.

In PHP 4.3, this function becomes binary safe.

Example 1

Search for a string with the ASCII value of "o" and return the remainder of the string:

<?php
echo stristr("Hello world!",111);
?>

Output:

o world!

Example 2

Return the part of the string before the first occurrence of "world":

<?php
echo stristr("Hello world!","WORLD",true);
?>

Output:

Hello

Example 3

Find the first occurrence of "world" in "Hello world!" and return the rest of the string:

<?php
echo stristr("Hello world!","WORLD");
?>

Output:

world!

The above is the detailed content of How to use php stristr function?. 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