PHP String strstr()函數(shù)
strstr()函數(shù)是PHP的內(nèi)置函數(shù)。愛掏網(wǎng) - it200.com它是一個(gè) 區(qū)分大小寫 的函數(shù),用于找到字符串的第一個(gè)出現(xiàn)位置。愛掏網(wǎng) - it200.comstrstr()主要用于在另一個(gè)字符串中搜索第一個(gè)字符串的出現(xiàn)位置,并顯示后者從前者的第一個(gè)出現(xiàn)位置開始的一部分。愛掏網(wǎng) - it200.com
它是一個(gè) 二進(jìn)制安全 函數(shù),意味著可以在不修改文件內(nèi)容的情況下對(duì)二進(jìn)制文件進(jìn)行操作。愛掏網(wǎng) - it200.comstrstr()函數(shù)類似于stristr()函數(shù),但唯一的區(qū)別是stristr()是不區(qū)分大小寫的函數(shù),而strstr()是區(qū)分大小寫的。愛掏網(wǎng) - it200.com
注意:strstr()是一個(gè)二進(jìn)制安全且區(qū)分大小寫的函數(shù)。愛掏網(wǎng) - it200.com
PHP strstr()函數(shù)的語法如下,包含三個(gè)參數(shù)。愛掏網(wǎng) - it200.com
stristr (string,search, $before_search)
參數(shù)
$string (必需): $string
是一個(gè)必需參數(shù),用于指定要搜索的字符串。愛掏網(wǎng) - it200.com換句話說,它是$ search
值在其中搜索的主字符串參數(shù)。愛掏網(wǎng) - it200.com
$search (必需): 這個(gè)函數(shù)的下一個(gè) 必需的 參數(shù)是search。愛掏網(wǎng) - it200.com它指定要在string參數(shù)中搜索的字符串。愛掏網(wǎng) - it200.com如果此參數(shù)包含一個(gè)數(shù)字或整數(shù)值而不是字符串,則它將搜索與該數(shù)字的ASCII值匹配的字符。愛掏網(wǎng) - it200.com
$before_search (可選): 這是strstr()函數(shù)的最后一個(gè)且 可選的 參數(shù),它指定布爾值,默認(rèn)值為 FALSE 。愛掏網(wǎng) - it200.com如果我們將其設(shè)置為TRUE,則它將返回在搜索參數(shù)的第一次出現(xiàn)之前的字符串部分。愛掏網(wǎng) - it200.com
返回值
PHP的strstr()函數(shù)返回剩余的字符串(從匹配點(diǎn)開始),如果找不到搜索的字符串,則返回 FALSE 。愛掏網(wǎng) - it200.com
技術(shù)細(xì)節(jié)
PHP版本支持 | PHP 4+版本支持此函數(shù)。愛掏網(wǎng) - it200.com |
---|---|
返回值 | strstr()函數(shù)返回字符串的剩余部分,如果搜索的字符串未找到,則返回False。愛掏網(wǎng) - it200.com |
更新日志 |
- 在PHP 5.3中,strstr()函數(shù)中添加了$before_search參數(shù)。愛掏網(wǎng) - it200.com
strstr()的示例
下面給出了一些示例,這些示例將幫助我們了解這個(gè)函數(shù)的實(shí)際用法。愛掏網(wǎng) - it200.com
//Returns remaining string after search found
Input:
string1 = "Hello! Good Morning everyone",search1 = "Good";
Output: Morning everyone
//case-sensitive, returns nothing
Input:
string = "Hello! Good Morning everyone ",search = "HELLO ";
Output:
//Returns initial string when search found
Input:
string = "Hello! Good Morning everyone",search = "Good", before_search = true;
Output: Hello!
//Passing ASCII value of r which is 114
Input:
string = "I want to travel the world ",search = "114", before_search = true;
Output: I want to t
下面給出了一些詳細(xì)的示例。愛掏網(wǎng) - it200.com通過這些示例,我們可以更好地理解該函數(shù)的用法。愛掏網(wǎng) - it200.com
示例1
這是一個(gè)簡單的strstr()示例,它顯示它是區(qū)分大小寫的函數(shù),因此如果字符串不匹配,則返回FALSE。愛掏網(wǎng) - it200.com它將返回$ search
變量在字符串中找到的位置之后的剩余字符串。愛掏網(wǎng) - it200.com
<?php
string = "Welcome to javaTpoint";search1 = "e";
echo strstr(string,search1);
echo '</br>';
search2 = "JAVA"; //case-sensitive
var_dump(strstr(string, search2));
echo '</br>';
var_dump(strstr(string, "WeLcOmE"));
?>
輸出:
示例2
在下面的示例中,我們將使用第三個(gè)參數(shù)$before_search
,其默認(rèn)值為 FALSE 。愛掏網(wǎng) - it200.com在這里,我們將將其設(shè)置為 TRUE ,它將返回字符串在第一次出現(xiàn)$search
參數(shù)之前的部分。愛掏網(wǎng) - it200.com如果未找到搜索字符串,則返回布爾值 false 。愛掏網(wǎng) - it200.com
<?php
string = "Welcome to javaTpoint website.";before_search = true;
search1 = "a"; echo strstr(string, search1,before_search);
echo '</br>';
search2 = "E";
var_dump(strstr(string, search2,before_search));
echo '</br>';
search3 = "nt";
echo strstr(string, search3,before_search);
?>
輸出:
示例3
我們還可以在search參數(shù)中傳遞整數(shù)值,而不是字符串或字符。愛掏網(wǎng) - it200.com這個(gè)整數(shù)值被視為ASCII值,并轉(zhuǎn)換為字符。愛掏網(wǎng) - it200.com在這個(gè)示例中,我們將在search中傳遞一個(gè)整數(shù)。愛掏網(wǎng) - it200.com
<?php
//119 is the ASCII value of lowercase w
echo strstr("There are seven wonders in the world.", 119, true);
echo '</br>';
//84 is the ASCII value of uppercase T
var_dump(strstr("There are seven wonders in the world.", 84));
echo '</br>';
//79 is the ASCII value of uppercase O
var_dump(strstr("There are seven wonders in the world.", 79));
?>
輸出:
注意:var_dump() 函數(shù)主要用于在瀏覽器中顯示布爾值。愛掏網(wǎng) - it200.com除了布爾值之外,它還會(huì)顯示參數(shù)的數(shù)據(jù)類型和參數(shù)中可用的字符數(shù)量,就像你在上面的示例中看到的那樣。愛掏網(wǎng) - it200.comecho 不足以打印這些信息。愛掏網(wǎng) - it200.com