본문 바로가기

php

[php] Finding a string contains a specific word - strpos(), stripos()

문자열 찾기를 원할 때 strpos(), stripos()를 사용한다.

 

특정 문자열 안에 내가 찾는 문자열이 포함되어 있는지 확인할 때 사용

문자열의 위치를 구할 때 사용

 

strpos( string, needle[, offset] ) - 대소문자 구별

stripos( string, needle[, offset] ) - 대소문자 구별 안함

$search_str = "I love my country";

$result_01 = strpos( $search_str, "country" ); // 10
$result_02 = strpos( $search_str, "korea" ); // false

 

Parameter:

- string: 검색할 문자열

- needle: 찾을 문자열

- [offset]: [옵션] 검색을 시작할 인덱스 지정

 

Return: 찾는 문자열이 처음 나타나는 인덱스 반환, 찾는 문자열이 없으면 FALSE 반환

 

* 인덱스는 0부터 시작

 

[기억법] String Position

 

[JavaScript 동일기능] https://shallwestudy.tistory.com/3