[php]strpos — 查找字符串首次出现的位置


官方範例1
<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);


// 注意這裡使用的是 ===。簡單的 == 不能像我們期待的那樣工作,
// 因為 'a' 是第 0 位置上的(第一個)字符。
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 " and exists at position $pos";
}
?>
官方範例2
<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);


// 使用 !== 操作符。使用 != 不能像我們期待的那樣工作,
// 因為 'a' 的位置是 0。語句 (0 != false) 的結果是 false。
if ($pos !== false) {
     echo "The string '$findme' was found in the string '$mystring'";
         echo " and exists at position $pos";
} else {
     echo "The string '$findme' was not found in the string '$mystring'";
}
?>


官方範例3
<?php
// 忽視位置偏移量之前的字符進行查找
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
?>

arrow
arrow
    全站熱搜

    stockwfj3 發表在 痞客邦 留言(0) 人氣()