라이브러리

[PHP] strcspn - 마스크와 일치하지 않는 초기 세그먼트의 길이 찾기




PHP의 strcspn 함수

PHP의 `strcspn` 함수는 문자열에서 특정 문자열이 포함되지 않는 최대 길이를 반환합니다. 이 함수는 `strspn` 함수와 유사하지만, `strspn` 함수는 특정 문자열이 포함된 최대 길이를 반환합니다.

함수 구조

`strcspn` 함수의 구조는 다음과 같습니다.

#hostingforum.kr
php

int strcspn ( string $haystack , string $needles )



* `$haystack` : 검색할 문자열
* `$needles` : 포함되지 않는 문자열

예제

#hostingforum.kr
php

// 문자열 "Hello, World!" 에서 ", "가 포함되지 않는 최대 길이를 반환합니다.

$haystack = "Hello, World!";

$needles = ", ";

echo strcspn($haystack, $needles); // 출력: 5



// 문자열 "Hello, World!" 에서 "o"가 포함되지 않는 최대 길이를 반환합니다.

$haystack = "Hello, World!";

$needles = "o";

echo strcspn($haystack, $needles); // 출력: 5



// 문자열 "Hello, World!" 에서 "z"가 포함되지 않는 최대 길이를 반환합니다.

$haystack = "Hello, World!";

$needles = "z";

echo strcspn($haystack, $needles); // 출력: 13



주의사항

* `strcspn` 함수는 `$needles` 문자열이 포함된 경우, 포함되지 않는 최대 길이를 반환합니다.
* `$needles` 문자열이 비어 있는 경우, `$haystack` 문자열의 전체 길이를 반환합니다.
* `$needles` 문자열이 포함된 경우, `$haystack` 문자열에서 `$needles` 문자열이 포함된 부분의 길이를 반환합니다.

예시 코드

#hostingforum.kr
php

function strcspn_example() {

    $haystack = "Hello, World!";

    $needles1 = ", ";

    $needles2 = "o";

    $needles3 = "z";



    echo "문자열 '$haystack' 에서 '$needles1' 가 포함되지 않는 최대 길이: " . strcspn($haystack, $needles1) . "
";

    echo "문자열 '$haystack' 에서 '$needles2' 가 포함되지 않는 최대 길이: " . strcspn($haystack, $needles2) . "
";

    echo "문자열 '$haystack' 에서 '$needles3' 가 포함되지 않는 최대 길이: " . strcspn($haystack, $needles3) . "
";

}



strcspn_example();



이 예시 코드는 `strcspn` 함수의 사용법을 보여줍니다. `$haystack` 문자열에서 `$needles1`, `$needles2`, `$needles3` 문자열이 포함되지 않는 최대 길이를 반환합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 120 페이지

검색

게시물 검색