strpos

(PHP 4, PHP 5, PHP 7)

strpos锟斤拷锟斤拷锟街凤拷锟斤拷锟阶次筹拷锟街碉拷位锟斤拷

说锟斤拷

strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int

锟斤拷锟斤拷 needle 锟斤拷 haystack 锟斤拷锟阶次筹拷锟街碉拷锟斤拷锟斤拷位锟矫★拷

锟斤拷锟斤拷

haystack

锟节革拷锟街凤拷锟斤拷锟叫斤拷锟叫诧拷锟揭★拷

needle

锟斤拷锟� needle 锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷么锟斤拷锟斤拷锟斤拷转锟斤拷为锟斤拷锟酵诧拷锟斤拷锟斤拷为锟街凤拷锟斤拷顺锟斤拷值锟斤拷

offset

锟斤拷锟斤拷峁╋拷舜瞬锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷始位锟矫匡拷始统锟狡★拷 锟斤拷锟斤拷歉锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷尾指锟斤拷锟街凤拷锟斤拷锟斤拷始锟斤拷

锟斤拷锟斤拷值

锟斤拷锟斤拷 needle 锟斤拷锟斤拷锟斤拷 haystack 锟街凤拷锟斤拷锟斤拷始锟斤拷位锟斤拷(锟斤拷锟斤拷锟斤拷 offset)锟斤拷同时注锟斤拷锟街凤拷锟斤拷位锟斤拷锟角达拷0锟斤拷始锟斤拷锟斤拷锟斤拷锟角达拷1锟斤拷始锟侥★拷

锟斤拷锟矫伙拷业锟� needle锟斤拷锟斤拷锟斤拷锟斤拷 FALSE锟斤拷

Warning

锟剿猴拷锟斤拷锟斤拷锟杰凤拷锟截诧拷锟斤拷值 FALSE锟斤拷锟斤拷也锟斤拷锟杰凤拷锟截碉拷同锟斤拷 FALSE 锟侥非诧拷锟斤拷值锟斤拷锟斤拷锟侥讹拷 锟斤拷锟斤拷锟斤拷锟斤拷锟铰斤拷锟皆伙拷取锟斤拷锟斤拷锟斤拷息锟斤拷应使锟斤拷 === 锟斤拷锟斤拷锟�锟斤拷锟斤拷锟皆此猴拷锟斤拷锟侥凤拷锟斤拷值锟斤拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
7.1.0 锟斤拷始支锟街革拷锟斤拷锟斤拷 offset锟斤拷

锟斤拷锟斤拷

Example #1 使锟斤拷 ===

<?php
$mystring 
'abc';
$findme   'a';
$pos strpos($mystring$findme);

// 注锟斤拷锟斤拷锟斤拷使锟矫碉拷锟斤拷 ===锟斤拷锟津单碉拷 == 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
// 锟斤拷为 'a' 锟角碉拷 0 位锟斤拷锟较的o拷锟斤拷一锟斤拷锟斤拷锟街凤拷锟斤拷
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";
}
?>

Example #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'";
}
?>

Example #3 使锟斤拷位锟斤拷偏锟斤拷锟斤拷

<?php
// 锟斤拷锟斤拷位锟斤拷偏锟斤拷锟斤拷之前锟斤拷锟街凤拷锟斤拷锟叫诧拷锟斤拷
$newstring 'abcdef abcdef';
$pos strpos($newstring'a'1); // $pos = 7, 锟斤拷锟斤拷 0
?>

注锟斤拷

Note: 锟剿猴拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟狡讹拷锟斤拷

锟轿硷拷

  • stripos() - 锟斤拷锟斤拷锟街凤拷锟斤拷锟阶次筹拷锟街碉拷位锟矫o拷锟斤拷锟斤拷锟街达拷小写锟斤拷
  • strrpos() - 锟斤拷锟斤拷指锟斤拷锟街凤拷锟斤拷锟斤拷目锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟揭伙拷纬锟斤拷值锟轿伙拷锟�
  • strripos() - 锟斤拷锟斤拷指锟斤拷锟街凤拷锟斤拷锟斤拷目锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟揭伙拷纬锟斤拷值锟轿伙拷茫锟斤拷锟斤拷锟斤拷执锟叫⌒达拷锟�
  • strstr() - 锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟阶次筹拷锟斤拷
  • strpbrk() - 锟斤拷锟街凤拷锟斤拷锟叫诧拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟轿猴拷一锟斤拷锟街凤拷
  • substr() - 锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟接达拷
  • preg_match() - 执锟斤拷匹锟斤拷锟斤拷锟斤拷锟斤拷式