PHP 字符串 strpos()函數
strops()是PHP的內置函數。愛掏網 - it200.com它用于找到一個字符串在另一個字符串或子字符串中第一次出現的位置。愛掏網 - it200.com
int strpos ( string haystack , mixedneedle [, int $offset = 0 ] );
參數 | 描述 | 必需/可選 |
---|---|---|
字符串 | 指定要搜索的字符串。愛掏網 - it200.com | 開始 |
查找 | 指定要查找的字符串。愛掏網 - it200.com | 必需 |
開始位置 | 指定搜索開始的位置。愛掏網 - it200.com | 可選 |
此函數將幫助我們在haystack字符串中找到needle的第一個出現的數值位置
注意:strops()函數區分大小寫,并且對二進制數據安全。愛掏網 - it200.com
示例1
<?php
str1="Hello Php";str2="Hello Php javatpoint!";
echo "First string is: ".str1;
echo "<br>";
echo "First string is: ".str2;
echo "<br>";
echo "By using 'strpos()' function:".strpos("str1,str2","php");
//str1 andstr2 'Php'first letter is upper case so output is nothing.
// It is case-sensitive
?>
輸出:
First string is: Hello Php
First string is: Hello Php javatpoint!
By using 'strpos()' function:
示例2
<?php
str1="Hello php";str2=" Hello php javatpoint!";
echo "First string is: ".str1;
echo "<br>";
echo "First string is: ".str2;
echo "<br>";
echo "By using 'strpos()' function:".strpos("str1,str2","php");
//Find the position of the first occurrence of "php" inside the string
?>
輸出:
First string is: Hello php
First string is: Hello php javatpoint!
By using 'strpos()' function:6
示例3
<?php
mystring = 'Hello PHP';findme = 'Hello';
pos = strpos(mystring, findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'Hello' was the 0th (first) character.
if (pos === false) {
echo "The string 'findme' was not found in the string 'mystring'";
} else {
echo "The string 'findme' was found in the string 'mystring'";
echo "<br>";
echo " and exists at position $pos";
}
?>
輸出:
The string 'Hello' was found in the string 'Hello PHP'
and exists at position 0
參見
strchr():用于在另一個字符串中查找第一個出現的字符串。愛掏網 - it200.com
strcoll() :基于區域設置的字符串比較。愛掏網 - it200.com
strcmp():二進制安全的字符串比較。愛掏網 - it200.com
參考:
http://php.net/manual/en/function.strpos.php
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。