Home > Article > Backend Development > PHP strtok()
Just like the other programming languages, PHP strtok() function of the PHP Programming Language helps in tokenizing the string into many smaller parts on some given delimeters basis. It helps in taking the input string as an argument along with the help of the delimiters which is nothing but as the second argument. PHP strtok() function is actually an inbuilt function and it is very much similar to the C strtok() function. The strtok() function helps in splitting a string into many tokens (smaller parts of the string) by taking the space character into consideration if it is necessary.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Strtok(string $str1, string $token1/$delimeter1)
Explanation of the Parameters:
The strtok() function of the PHP coding language usually accepts only two parameters and both of those are mandatory and are need to be passed. One parameter is $str1 parameter and the second one is $token1/$delimeter1 parameter.
Return value of the strtok() function:
The strtok() function actually returns a string or group of strings which are separated with the help of the delimeters/tokens. These series/groups of string will be found with the help of the strtok() function usage in the while loop.
Given below are the examples:
This is the example of implementing strtok() function with the help of “space” value as $delimeter1 parameter and “Pavan Kumar Sake” as the $str1 parameter’s value. At first, $str1 variable is created with a string value “Pavan Kumar Sake” then $delimeter variable is created with “space”. Then $token1 variable is created with strtok() function along with its two parameters. Then while() function is created with the condition (token1 value is not a FALSE value). If the condition is TRUE then echo statement will print $token1 variable value by splitting the string at the space element’s exact point.
Syntax:
<?php $str1 = "Pavan Kumar Sake"; $delimeter1 = " "; $token1 = strtok($str1, $delimeter1); while ($token1 !== false) { echo "$token1 <br>"; $token1 = strtok($delimeter1); } ?>
Output:
This is the example of implementing strtok() function with the help of “,” special character value as $delimeter11 parameter and “Hi,Pavan Kumar Sake Blogger” as the $str11 parameter’s value. At first, $str11 variable is created with a string value “Hi,Pavan Kumar Sake Blogger” then $delimeter11 variable is created with “,” special character. Then $token11 variable is created with strtok() function along with its two parameters. Then while() function is created with the condition (token11 value is not a FALSE value). If the condition is TRUE then echo statement will print $token11 variable value by splitting the string at the “,” special character element’s exact point.
Syntax:
<?php $str11 = "Hi,PavanKumarSake Blogger"; $delimeter11 = ","; $token11 = strtok($str11, $delimeter11); while ($token11 !== false) { echo "$token11 <br>"; $token11 = strtok($delimeter11); } ?>
Output:
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:
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:
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.
The above is the detailed content of PHP strtok(). For more information, please follow other related articles on the PHP Chinese website!