PHP substr() 함수
예시
문자열에서 "world"를 반환합니다.
<?php
echo substr("Hello world",6);
?>
정의 및 사용
substr() 함수는 문자열의 일부를 반환합니다.
통사론
substr(string,start,length)
매개변수 값
Parameter | Description |
---|---|
string | Required. Specifies the string to return a part of |
start | Required. Specifies where to start in the string
|
length | Optional. Specifies the length of the returned string. Default is to the end of the string.
|
기술적 세부 사항
반환 값: | 문자열의 추출된 부분을 반환하거나 실패 시 FALSE를 반환하거나 빈 문자열을 반환합니다. |
---|---|
PHP 버전: | 4+ |
변경 로그: | PHP 7.0 - string = start (문자 길이)인 경우 빈 문자열을 반환합니다. 이전 버전은 FALSE를 반환합니다. PHP 5.2.2 - 5.2.6 - start 가 음수 잘림의 위치에 있으면 FALSE가 반환됩니다. 다른 버전은 처음부터 문자열을 가져옵니다. |
더 많은 예
예시
다른 양수 및 음수와 함께 시작 매개변수 사용:
<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
예시
양수와 음수가 다른 시작 및 길이 매개변수 사용:
<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>
❮ PHP 문자열 참조