php 에서 instr 과 비슷한 역할을 하는 함수들 - strpos, strrpos, stripos, strripos
페이지 정보
본문
우선, 약자에 대해서 알아 보면,
r 는 reverse 를 말한다. - 뒤에서부터
i 는 insensitive 를 말한다. - 대소문자 구분하지 않고
Unlike the strpos(), stripos() is case-insensitive.
strpos() - Find the position of the first occurrence of a substring in a string
strrpos() - Find the position of the last occurrence of a substring in a string
stripos() - Find the position of the first occurrence of a case-insensitive substring in a string
strripos() - Find the position of the last occurrence of a case-insensitive substring in a string
ㅁ strpos
ㅁ strrpos
ㅁ stripos
ㅁ strripos
예제 ¶
Example #1 === 사용하기
[code]
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// === 를 사용하는 점에 주의하십시오. == 는 'a'가 0번째
// (처음) 문자이기에 기대하는 대로 작동하지 않습니다.
if ($pos === false) {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾지 못했습니다.";
} else {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾았습니다.";
echo "위치 $pos에 존재합니다.";
}
?>
[/code]
Example #2 !== 사용하기
[code]
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// !== 연산자를 사용할 수 있습니다. !=는 'a'의 위치가
// 0이기 때문에 원하는 동작을 얻을 수 없습니다.
// 구문 (0 != false)는 false로 평가됩니다.
if ($pos !== false) {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾았습니다.";
echo "위치 $pos에 존재합니다.";
} else {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾지 못했습니다.";
}
?>
[/code]
r 는 reverse 를 말한다. - 뒤에서부터
i 는 insensitive 를 말한다. - 대소문자 구분하지 않고
Unlike the strpos(), stripos() is case-insensitive.
strpos() - Find the position of the first occurrence of a substring in a string
strrpos() - Find the position of the last occurrence of a substring in a string
stripos() - Find the position of the first occurrence of a case-insensitive substring in a string
strripos() - Find the position of the last occurrence of a case-insensitive substring in a string
ㅁ strpos
ㅁ strrpos
ㅁ stripos
ㅁ strripos
예제 ¶
Example #1 === 사용하기
[code]
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// === 를 사용하는 점에 주의하십시오. == 는 'a'가 0번째
// (처음) 문자이기에 기대하는 대로 작동하지 않습니다.
if ($pos === false) {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾지 못했습니다.";
} else {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾았습니다.";
echo "위치 $pos에 존재합니다.";
}
?>
[/code]
Example #2 !== 사용하기
[code]
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// !== 연산자를 사용할 수 있습니다. !=는 'a'의 위치가
// 0이기 때문에 원하는 동작을 얻을 수 없습니다.
// 구문 (0 != false)는 false로 평가됩니다.
if ($pos !== false) {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾았습니다.";
echo "위치 $pos에 존재합니다.";
} else {
echo "'$findme' 문자열을 '$mystring' 문자열에서 찾지 못했습니다.";
}
?>
[/code]
추천0
댓글목록
등록된 댓글이 없습니다.