>  기사  >  백엔드 개발  >  PHP strtok()

PHP strtok()

WBOY
WBOY원래의
2024-08-29 12:53:58220검색

다른 프로그래밍 언어와 마찬가지로 PHP 프로그래밍 언어의 PHP strtok() 함수는 특정 구분 기호를 기준으로 문자열을 더 작은 부분으로 토큰화하는 데 도움이 됩니다. 두 번째 인수에 불과한 구분 기호의 도움과 함께 입력 문자열을 인수로 사용하는 데 도움이 됩니다. PHP strtok() 함수는 실제로 내장 함수이며 C strtok() 함수와 매우 유사합니다. strtok() 함수는 필요한 경우 공백 문자를 고려하여 문자열을 여러 토큰(문자열의 작은 부분)으로 분할하는 데 도움이 됩니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문:

Strtok(string $str1, string $token1/$delimeter1)

매개변수 설명:

PHP 코딩 언어의 strtok() 함수는 일반적으로 두 개의 매개변수만 허용하며 두 매개변수는 모두 필수이므로 전달해야 합니다. 하나의 매개변수는 $str1 매개변수이고 두 번째 매개변수는 $token1/$delimeter1 매개변수입니다.

  • $str1: PHP 프로그래밍 언어의 strtok() 함수의 $str1 매개변수는 실제로 제공되는 입력 문자열을 나타내는 데 도움이 됩니다. 실제 주어진 문자열 값입니다. strtok() 매개변수는 필수 매개변수입니다.
  • $token1/$delimeter1: PHP 프로그래밍 언어의 strtok() 함수의 $token1/$delimeter1 매개 변수는 구분 문자(분할 문자 또는 토큰 문자)를 나타내는 데 도움이 됩니다. 이 strtok() 매개변수도 필수 매개변수입니다.

strtok() 함수의 반환 값:

strtok() 함수는 실제로 구분 기호/토큰을 사용하여 구분된 문자열 또는 문자열 그룹을 반환합니다. 이러한 일련의 문자열/그룹은 while 루프에서 strtok() 함수 사용을 통해 찾을 수 있습니다.

PHP에서 strtok() 함수는 어떻게 작동하나요?

  • PHP 프로그래밍 언어의 strtok() 함수는 실제로 필수이고 전달되어야 하는 단 두 개의 매개변수를 사용하여 일반 문자열을 여러 개의 작은 문자열로 분할하는 방식으로 작동합니다.
  • 하나는 문자열 매개변수이고 두 번째는 구분 기호/토큰 매개변수입니다.
  • 구분 기호 매개변수는 문자열 값을 여러 개의 작은 문자열 요소로 분할하는 데 도움이 됩니다.
  • 문자열 값은 알파벳, 문자, 공백 등이 될 수 있습니다.
  • 문자열은 지정된 delimeter1 매개변수 값의 정확한 지점에서만 분할됩니다.
  • delimeter1 매개변수는 문자열을 하나의 큰 문장에서 작은 문자열로 분할하는 데 도움이 되는 다양한 유형의 문자, 알파벳, 숫자 값을 가질 수 있습니다.

PHP strtok()의 예

다음은 예시입니다.

예시 #1

$delimeter1 매개변수로 "space" 값을, $str1 매개변수 값으로 "Pavan Kumar Sake"를 사용하여 strtok() 함수를 구현한 예입니다. 처음에는 $str1 변수가 문자열 값 "Pavan Kumar Sake"로 생성된 다음 $delimeter 변수가 "space"로 생성됩니다. 그런 다음 $token1 변수는 두 개의 매개변수와 함께 strtok() 함수를 사용하여 생성됩니다. 그런 다음 조건(token1 값이 FALSE 값이 아님)을 사용하여 while() 함수가 생성됩니다. 조건이 TRUE이면 echo 문은 공백 요소의 정확한 지점에서 문자열을 분할하여 $token1 변수 값을 인쇄합니다.

구문:

<?php
$str1 = "Pavan Kumar Sake";
$delimeter1 = " ";
$token1 = strtok($str1, $delimeter1);
while ($token1 !== false)
{
echo "$token1 <br>";
$token1 = strtok($delimeter1);
}
?>

출력:

PHP strtok()

예시 #2

$delimeter11 매개변수에 "," 특수문자 값을, $str11 매개변수에 "Hi,Pavan Kumar Sake Blogger"를 사용하여 strtok() 함수를 구현한 예입니다. 처음에는 $str11 변수가 "Hi,Pavan Kumar Sake Blogger" 문자열 값으로 생성된 다음 $delimeter11 변수가 "," 특수 문자로 생성됩니다. 그런 다음 $token11 변수는 두 개의 매개변수와 함께 strtok() 함수를 사용하여 생성됩니다. 그런 다음 조건(token11 값이 FALSE 값이 아님)을 사용하여 while() 함수가 생성됩니다. 조건이 TRUE이면 echo 문은 "," 특수 문자 요소의 정확한 지점에서 문자열을 분할하여 $token11 변수 값을 인쇄합니다.

구문:

<?php
$str11 = "Hi,PavanKumarSake Blogger";
$delimeter11 = ",";
$token11 = strtok($str11, $delimeter11);
while ($token11 !== false)
{
echo "$token11 <br>";
$token11 = strtok($delimeter11);
}
?>

출력:

PHP strtok()

Example #3

This is the example of implementing strtok() function of PHP with the help of “space” value as $delimeter11 parameter and “Hellow World! Its?SakePavan Kumar” as the $string11 parameter’s value. At first, $string11 variable is created with a string value “Hellow World! Its?SakePavan Kumar Sake” then $delimeter11 variable is created with “space” element. Then $token111 variable is created with strtok() function along with its two parameters. Then while() function inside of PHP tag is created with the condition (token111 value is not a FALSE value). If the condition inside while loop is TRUE then echo statement will print $token111 variable value by splitting the given/provided string at the space element’s exact point. You can check the output below to know how the splitting of the string sentence into smaller string elements.

Syntax:

<?php
$string11 = "Hellow World! Its?SakePavan Kumar.";
$delimiter11 =  " ";
$token111 = strtok($string11, $delimiter11);
while($token111 !==false)
{
echo $token111. "<br>";
$token111 =strtok($delimiter11);
}
?>

Output:

PHP strtok()

Example #4

This is the example of implementing strtok() function of PHP coding language with the help of “e” alphabet value as $delimeter1 parameter ($del11 variable value) and “Hey Buddy! Welcome to PavanKumarSake Park” as the $str111 parameter’s value. At first, $str111 variable is created with a string value “Hey Buddy! Welcome to PavanKumarSake Park” then $del11 variable is created with “e” alphabet character value. Then $token11 variable is created with strtok() function along with its two parameters($str111 and $del11 parameters). Then while() function is created with the condition ($token11 value is not a FALSE value). If the condition of the loop is TRUE then echo statement of the php will print $token11 variable value by splitting the string at the “e” alphabet characters element’s exact point.

Syntax:

<?php
$str111 = "Hey Buddy! Welcome to PavanKumarSake Park";
$del11 =  "e";
$token11 = strtok($str111, $del11);
while($token11 !==false)
{
echo $token11. "<br>";
$token11=strtok($del11);
}
?>

Output:

PHP strtok()

Conclusion

Here we saw what is the definition of strtok() function of the PHP Programming Language along with its syntax and various parameters explanation, how strtok() function works in PHP along with various examples of implementation to know how strtok() function works.

위 내용은 PHP strtok()의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP is_null()다음 기사:PHP is_null()