就像其他编程语言一样,PHP 编程语言的 PHP strtok() 函数有助于根据给定的分隔符将字符串标记为许多更小的部分。它有助于将输入字符串作为参数以及分隔符的帮助,而分隔符只不过是作为第二个参数。 PHP strtok() 函数实际上是一个内置函数,它与 C strtok() 函数非常相似。 strtok() 函数通过在必要时考虑空格字符来帮助将字符串拆分为许多标记(字符串的较小部分)。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
Strtok(string $str1, string $token1/$delimeter1)
参数说明:
PHP 编码语言的 strtok() 函数通常只接受两个参数,并且这两个参数都是强制性的,需要传递。第一个参数是 $str1 参数,第二个参数是 $token1/$delimeter1 参数。
strtok()函数的返回值:
strtok() 函数实际上返回一个字符串或一组在分隔符/标记的帮助下分隔的字符串。这些字符串/字符串组将在 while 循环中使用 strtok() 函数找到。
以下是示例:
这是借助“space”值作为 $delimeter1 参数和“Pavan Kumar Sake”作为 $str1 参数值来实现 strtok() 函数的示例。首先,使用字符串值“Pavan Kumar Sake”创建 $str1 变量,然后使用“space”创建 $delimeter 变量。然后使用 strtok() 函数及其两个参数创建 $token1 变量。然后,使用条件(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); } ?>
输出:
这是借助“,”特殊字符值作为 $delimeter11 参数和“Hi,Pavan Kumar Sake Blogger”作为 $str11 参数值来实现 strtok() 函数的示例。首先,使用字符串值“Hi,Pavan Kumar Sake Blogger”创建 $str11 变量,然后使用“,”特殊字符创建 $delimeter11 变量。然后使用 strtok() 函数及其两个参数创建 $token11 变量。然后,使用条件(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); } ?>
输出:
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.
以上是PHP strtok()的详细内容。更多信息请关注PHP中文网其他相关文章!