mirror of
https://github.com/php/doc-ja.git
synced 2026-04-25 17:08:10 +02:00
0d68801773
ereg系の関数は、PHP 5.3.0 で非推奨になり、7.0.0 で削除されている。 ドキュメントも完全に削除され、なかったことにされているので注意。 https://github.com/php/doc-en/commit/8f5f6a52c0d25a42a069c6c1cff4effb68fb379c
200 lines
6.1 KiB
XML
200 lines
6.1 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: d335ba69a16f4013280de8e3e71d9ba19fe3cb3c Maintainer: hirokawa Status: ready -->
|
|
<!-- CREDITS: shimooka,mumumu -->
|
|
<refentry xml:id="function.strtok" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<refnamediv>
|
|
<refname>strtok</refname>
|
|
<refpurpose>文字列をトークンに分割する</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type class="union"><type>string</type><type>false</type></type><methodname>strtok</methodname>
|
|
<methodparam><type>string</type><parameter>string</parameter></methodparam>
|
|
<methodparam><type>string</type><parameter>token</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<simpara>代替のシグネチャ (名前付き引数をサポートしていません):</simpara>
|
|
<methodsynopsis>
|
|
<type class="union"><type>string</type><type>false</type></type><methodname>strtok</methodname>
|
|
<methodparam><type>string</type><parameter>token</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
<function>strtok</function> は文字列 (<parameter>string</parameter>)
|
|
を 何らかの文字 <parameter>token</parameter>
|
|
によって区切られている小さな文字列 (トークン) に分割します。
|
|
"This is an example string" のような文字列がある場合、
|
|
空白文字を <parameter>token</parameter> に指定することでこの文字列を個々の単語に分割することができます。
|
|
</para>
|
|
<para>
|
|
strtok は最初のコールの時のみ <parameter>string</parameter> 引数を使用することに注意してください。
|
|
strtok は、文字列のどこにいるのかの情報を保持しているため、
|
|
2回目以降のコールでは <parameter>token</parameter> のみが必要です。
|
|
最初からやりなおす場合や新しい文字列をトークンに分割する場合、
|
|
初期化するために再度 <parameter>string</parameter> 引数を指定して strtok をコールします。
|
|
文字列は、<parameter>token</parameter> の文字のどれかが見つかった場合はトークンに分割されます。
|
|
</para>
|
|
<note>
|
|
<para>
|
|
この関数は、<function>explode</function>
|
|
関数に慣れている人が期待する動きとは、
|
|
ちょっと異なる振る舞いをします。
|
|
まず、<parameter>token</parameter> の
|
|
ふたつ以上の連続する文字が文字列に含まれていた場合、
|
|
その連続する文字は、単一の区切り文字として扱われます。
|
|
また、文字列の最初と最後に存在する <parameter>token</parameter>
|
|
は無視されてしまいます。
|
|
たとえば、文字列が <literal>";aaa;;bbb;"</literal> の場合、
|
|
<parameter>token</parameter> に
|
|
<literal>";"</literal> を指定して <function>strtok</function> を
|
|
連続してコールすると、
|
|
"aaa" と "bbb" を返し、最後に &false; を返します。
|
|
結果として、文字列はふたつにしか分割されません。
|
|
一方で <literal>explode(";", $string)</literal>
|
|
は 5つの要素からなる配列を返します。
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>string</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
より小さい文字列 (トークン) に分割する文字列。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>token</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
<parameter>string</parameter> を分割する際に使用する区切り文字。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
トークンを文字列で返します。
|
|
トークンがない場合は、&false; を返します。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>strtok</function> の例</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$string = "This is\tan example\nstring";
|
|
/* タブと改行をトークンの区切りとして使用します */
|
|
$tok = strtok($string, " \n\t");
|
|
|
|
while ($tok !== false) {
|
|
echo "Word=$tok<br />";
|
|
$tok = strtok(" \n\t");
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>空の部分が見つかった場合の <function>strtok</function> の動作</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$first_token = strtok('/something', '/');
|
|
$second_token = strtok('/');
|
|
var_dump($first_token, $second_token);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
string(9) "something"
|
|
bool(false)
|
|
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>strtok</function> と <function>explode</function> の違い</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$string = ";aaa;;bbb;";
|
|
$parts = [];
|
|
$tok = strtok($string, ";");
|
|
while ($tok !== false) {
|
|
$parts[] = $tok;
|
|
$tok = strtok(";");
|
|
}
|
|
echo json_encode($parts),"\n";
|
|
$parts = explode(";", $string);
|
|
echo json_encode($parts),"\n";
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
["aaa","bbb"]
|
|
["","aaa","","bbb",""]
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
|
|
&return.falseproblem;
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>explode</function></member>
|
|
</simplelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|