首頁  >  文章  >  後端開發  >  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 is_null()