検索

他のプログラミング言語と同様に、PHP プログラミング言語の PHP strtok() 関数は、特定の区切り文字に基づいて文字列を多くの小さな部分にトークン化するのに役立ちます。これは、2 番目の引数としてのみ使用される区切り文字の助けとともに、入力文字列を引数として受け取るのに役立ちます。 PHP strtok() 関数は実際には組み込み関数であり、C の strtok() 関数と非常によく似ています。 strtok() 関数は、必要に応じてスペース文字を考慮して、文字列を多数のトークン (文字列のより小さい部分) に分割するのに役立ちます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

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

パラメータの説明:

PHP コーディング言語の strtok() 関数は、通常 2 つのパラメータのみを受け入れます。これらのパラメータは両方とも必須であり、渡す必要があります。 1 つのパラメータは $str1 パラメータで、2 つ目のパラメータは $token1/$delimeter1 パラメータです。

  • $str1: PHP プログラミング言語の strtok() 関数の $str1 パラメーターは、実際に提供される入力文字列を表すのに役立ちます。これは、実際に指定された文字列値です。この strtok() パラメータは必須パラメータです。
  • $token1/$delimeter1: PHP プログラミング言語の strtok() 関数の $token1/$delimeter1 パラメーターは、区切り文字 (分割文字またはトークン文字) を表すのに役立ちます。この strtok() パラメータも必須パラメータです。

strtok() 関数の戻り値:

strtok() 関数は実際には、区切り文字/トークンを使用して区切られた文字列または文字列のグループを返します。これらの文字列/グループは、while ループ内で strtok() 関数を使用することで見つかります。

PHP での strtok() 関数はどのように動作しますか?

  • PHP プログラミング言語の strtok() 関数は、実際に必須で渡す必要がある 2 つのパラメーターのみを使用して、通常の文字列を多数の小さな文字列に分割することに基づいて機能します。
  • 1 つは文字列パラメーターで、2 つ目は区切り文字/トークン パラメーターです。
  • 区切りパラメータは、文字列値を多数の小さな文字列要素に分割するのに役立ちます。
  • 文字列値には、任意のアルファベット、任意の文字、スペースなどを使用できます。
  • 文字列は、指定された delimeter1 パラメーター値の正確な位置でのみ分割されます。
  • delimeter1 パラメータには、文字列を 1 つの大きな文から小さな文字列に分割するのに役立つ、さまざまな種類の文字、アルファベット、数値を含めることができます。

PHP strtok() の例

以下に例を示します:

例 #1

これは、$delimeter1 パラメーターとして「space」値、$str1 パラメーターの値として「Pavan Kumar Sake」を使用して strtok() 関数を実装する例です。まず、文字列値「Pavan Kumar Sake」を使用して $str1 変数が作成され、次に「スペース」を使用して $delimeter 変数が作成されます。次に、strtok() 関数とその 2 つのパラメーターを使用して $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 パラメーターとして「,」特殊文字値を使用し、$str11 パラメーターの値として「Hi,Pavan Kumar Sake Blogger」を使用して strtok() 関数を実装する例です。まず、文字列値「Hi,Pavan Kumar Sake Blogger」を使用して $str11 変数が作成され、次に特殊文字「,」を使用して $delimeter11 変数が作成されます。次に、strtok() 関数とその 2 つのパラメーターを使用して $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 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境