首页  >  文章  >  后端开发  >  PHP strtok()

PHP strtok()

WBOY
WBOY原创
2024-08-29 12:53:58221浏览

就像其他编程语言一样,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() 函数找到。

strtok() 函数在 PHP 中如何工作?

  • PHP 编程语言的 strtok() 函数的工作原理是借助仅有的两个实际上是必需且需要传递的参数,将普通字符串拆分为许多较小的字符串。
  • 第一个是字符串参数,第二个是分隔符/标记参数。
  • Delimeter 参数有助于将字符串值拆分为许多较小的字符串元素。
  • 字符串值可以是任何字母或任何字符或空格或类似的东西。
  • 字符串将仅在指定的 delimeter1 参数值的精确点处进行分割。
  • delimeter1 参数可以有不同类型的字符、字母、数值,这有助于将单个大句子中的字符串拆分为小字符串。

PHP strtok() 示例

以下是示例:

示例#1

这是借助“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);
}
?>

输出:

PHP strtok()

示例#2

这是借助“,”特殊字符值作为 $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);
}
?>

输出:

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 bin2hex()