PHP 버전
4+
$str_filename = 'image.test.png';
$str_extension = '.';

$extension_position = strrpos($str_filename, $str_extension);

if ($extension_position !== false) {
    echo "찾은 위치: $extension_position";
} else {
    echo '찾을 수 없습니다.';
}

// 출력: 찾은 위치: 10

/*
 * 주의하세요!
 * 문자열에서 인덱스는 0부터 시작합니다.
 * 첫 번째 문자열의 인덱스는 0이고, 두 번째 문자열의 인덱스는 1입니다.
*/
strrpos(string $haystack, string $needle, int $offset = 0): int|false
$str = 'Hello, World!';
$substring = 'world';

$pos = strrpos($str, $substring);

if ($pos !== false) {
    echo "문자열에 'world!'가 포함되어 있습니다.";
} else {
    echo "대소문자가 일치하지 않아 'world'를 찾을 수 없습니다.";
}

// 출력: "대소문자가 일치하지 않아 'world'를 찾을 수 없습니다."
$newstring = 'abcdef ghijk';
$pos = strrpos($newstring, 'a');

var_dump($pos); // int(0)
$str = 'Hello, World!';
$substring = 'Hello';

$pos = strrpos($str, $substring);

var_dump($pos); // int(0)

if ($pos === false) {
    echo "문자열에 'Hello'를 찾을 수 없습니다.";
} else {
    echo "문자열에 'Hello'가 포함되어 있습니다.";
}

// 출력: "문자열에 'Hello'가 포함되어 있습니다."

/* 또는 */

if ($pos !== false) {
    echo "문자열에 'Hello'가 포함되어 있습니다.";
} else {
    echo "문자열에 'Hello'를 찾을 수 없습니다.";
}

// 출력: "문자열에 'Hello'가 포함되어 있습니다."
$str = '반갑습니다. 환영합니다!';
$substring = '환영';

$pos = strrpos($str, $substring);

if ($pos !== false) {
    echo "찾은 위치: $pos";
} else {
    echo '찾을 수 없습니다.';
}

// 출력: 찾은 위치: 17 <= 원하지 않는 결과가 발생합니다.
$filename = 'image.test.png'; // 파일명

$dot_position = strrpos($filename, '.'); // 파일명에서 마지막 . 위치 찾기

if ($dot_position !== false) {
    $extension = substr($filename, $dot_position + 1); // 마지막 . 위치 바로 다음부터 끝까지의 문자열 추출하기
    echo "파일 확장자: $extension";
} else {
    echo '확장자를 찾을 수 없습니다.';
}

// 출력: 파일 확장자: png
$filepath = '/var/www/html/images/photo.jpg'; // 파일 경로

$slash_position = strrpos($filepath, '/'); // 파일 경로에서 마지막 / 위치 찾기

if ($slash_position !== false) {
    $filename = substr($filepath, $slash_position + 1); // 마지막 / 위치 바로 다음부터 끝까지의 문자열 추출하기
    echo "파일명: $filename";
} else {
    echo '파일명을 찾을 수 없습니다.';
}

// 출력: 파일명: photo.jpg
$str = 'PHP is a popular programming language'; // 문자열

$space_position = strrpos($str, ' '); // 문자열에서 마지막 공백 위치 찾기

if ($space_position !== false) {
    $last_word = substr($str, $space_position + 1); // 마지막 공백 위치 바로 다음부터 끝까지의 문자열 추출하기
    echo "마지막 단어: $last_word";
} else {
    echo '마지막 단어를 찾을 수 없습니다.';
}

// 출력: 마지막 단어: language
$url = 'https://example.com/products/item/detail'; // URL

$last_slash_position = strrpos($url, '/'); // URL에서 마지막 / 위치 찾기

if ($last_slash_position !== false) {
    $last_path = substr($url, $last_slash_position + 1); // 마지막 / 위치 바로 다음부터 끝까지의 문자열 추출하기
    echo "마지막 경로: $last_path";
} else {
    echo '마지막 경로를 찾을 수 없습니다.';
}

// 출력: 마지막 경로: detail