Hướng dẫn
Quảng cáo

Hàm stristr() trong PHP

Hướng dẫn cách sử dụng hàm stristr() trong lập trình PHP

Tác dụng của hàm stristr()

The stristr() function find the first occurrence of a string within another string.

This function is case-insensitive. For case-sensitive searches, use the strstr() function.

The following table summarizes the technical details of this function.

Return Value: Returns the portion of string, or FALSE if the string is not found.
Changelog: Since PHP 7.3.0, passing an integer as search parameter has been deprecated.
Version: PHP 4+

Syntax

The basic syntax of the stristr() function is given with:

stristr(string, search, before_search);

The following example shows the stristr() function in action.

<?php
// Sample string
$str = "Rain Rain Go Away";

// Searching for the substring
echo stristr($str, "go");
?>

Tip: If you simply want to find out if a particular substring occurs within a string or not, use the faster and less memory intensive function strpos() instead.


Parameters

The stristr() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to search.
search Required. Specifies the string to search for.
before_search Optional. If set to true, it returns the part of the string before the first occurrence of the search string. Default value is false which returns all of the string after the first occurrence of the search string (including search string itself).

More Examples

Here're some more examples showing how stristr() function actually works:

The following example returns the part of the string before the first occurrence of substring.

<?php
// Sample string
$str = "Rain Rain Go Away";

// Searching for the substring
echo stristr($str, "go", true);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements