PHP Velho Oeste 2024

stripos

(PHP 5, PHP 7)

stripos대소문자를 구분 없이 문자열이 처음 나타나는 위치를 탐색

설명

int stripos ( string $haystack , string $needle [, int $offset ] )

haystack string에서 needle이 처음 나타나는 위치를 반환합니다.

strpos()와 달리, stripos()는 대소문자를 구분하지 않습니다.

인수

haystack

검색할 문자열

needle

needle는 한 문자 이상의 문자열이여야 합니다.

needle이 문자열이 아니라면, 정수로 변환하여 해당하는 값의 문자를 적용합니다.

offset

선택적인 offset 인수로 haystack의 몇번째 문자에서 검색을 시작할지 지정할 수 있습니다. 반환하는 위치는 haystack의 처음부터 세어집니다.

반환값

needle을 발견하지 못하면, stripos()boolean FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

예제

Example #1 stripos() 예제

<?php
$findme    
'a';
$mystring1 'xyz';
$mystring2 'ABC';

$pos1 stripos($mystring1$findme);
$pos2 stripos($mystring2$findme);

// 'a'는 'xyz' 안에 없습니다.
if ($pos1 === false) {
    echo 
"문자열 '$findme'은 문자열 '$mystring1'에서 발견할 수 없습니다.";
}

// ===를 사용하는 점에 주의하십시오. ==는 'a'가 0번째
// (처음) 문자기에, 기대하는대로 작동하지 않습니다.
if ($pos2 !== false) {
    echo 
"'$findme'를 '$mystring2' 위치 $pos2에서 찾았습니다.";
}
?>

주의

Note: 이 함수는 바이너리 안전입니다.

참고

  • strpos() - 문자열이 처음 나타나는 위치를 찾습니다
  • strrpos() - 문자열에서 마지막 문자의 위치를 찾습니다
  • strrchr() - 문자열에서 문자가 마지막으로 나오는 부분을 찾습니다
  • substr() - Return part of a string
  • stristr() - 대소문자를 구분하지 않는 strstr
  • strstr() - 문자열이 처음으로 나오는 부분을 찾습니다
  • stripos()
  • str_ireplace() - 대소문자 구분 없는 str_replace

add a note add a note

User Contributed Notes 5 notes

up
43
emperorshishire at gmail dot com
15 years ago
I found myself needing to find the first position of multiple needles in one haystack.  So I wrote this little function:

<?php
function multineedle_stripos($haystack, $needles, $offset=0) {
    foreach(
$needles as $needle) {
       
$found[$needle] = stripos($haystack, $needle, $offset);
    }
    return
$found;
}

// It works as such:
$haystack = "The quick brown fox jumps over the lazy dog.";
$needle = array("fox", "dog", ".", "duck")
var_dump(multineedle_stripos($haystack, $needle));
/* Output:
   array(3) {
     ["fox"]=>
     int(16)
     ["dog"]=>
     int(40)
     ["."]=>
     int(43)
     ["duck"]=>
     bool(false)
   }
*/
?>
up
6
sorrynorealemail at example dot com
5 years ago
Unlike strpos() it seems that stripos() does NOT issue a WARNING if the needle is an empty string ''.
up
4
spam at kleppinger dot com
9 years ago
Regarding the function by spam at wikicms dot org

It is very bad practice to use the same function name as an existing php function but have a different output format.  Someone maintaining the code in the future is likely to be very confused by this.  It will also be hard to eradicate from a codebase because the naming is identical so each use of stripos() would have to be analyzed to see how it is expecting the output format (bool or number/bool).

Calling it string_found() or something like that would make a lot more sense for long-term use.
up
4
Ian Macdonald
8 years ago
Regarding the === note, it might be worth clarifying that the correct tests for a binary found/not found condition are  !==false to detect found, and ===false to detect not found.
up
2
emanuel dot karlsson at rolfsbuss dot se
5 years ago
Finding numbers in strings requires you to cast the number to string first.

strpos("123", 2) !== strpos("123", "2")
To Top