reference/tokenizer/functions/token-get-all.xml
82c84a325ea8d03f45669988e4f6ed53221ebd8f
...
...
@@ -9,10 +9,11 @@
9
9
&reftitle.description;
10
10
<methodsynopsis>
11
11
<type>array</type><methodname>token_get_all</methodname>
12
-
<methodparam><type>string</type><parameter>source</parameter></methodparam>
12
+
<methodparam><type>string</type><parameter>code</parameter></methodparam>
13
+
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
13
14
</methodsynopsis>
14
15
<para>
15
-
<function>token_get_all</function> parses the given <parameter>source</parameter>
16
+
<function>token_get_all</function> parses the given <parameter>code</parameter>
16
17
string into PHP language tokens using the Zend engine&apos;s lexical scanner.
17
18
</para>
18
19
<para>
...
...
@@ -26,13 +27,29 @@
26
27
<para>
27
28
<variablelist>
28
29
<varlistentry>
29
-
<term><parameter>source</parameter></term>
30
+
<term><parameter>code</parameter></term>
30
31
<listitem>
31
32
<para>
32
33
The PHP source to parse.
33
34
</para>
34
35
</listitem>
35
36
</varlistentry>
37
+
<varlistentry>
38
+
<term><parameter>flags</parameter></term>
39
+
<listitem>
40
+
<para>
41
+
Valid flags:
42
+
<itemizedlist>
43
+
<listitem>
44
+
<simpara>
45
+
<constant>TOKEN_PARSE</constant> - Recognises the ability to use
46
+
reserved words in specific contexts.
47
+
</simpara>
48
+
</listitem>
49
+
</itemizedlist>
50
+
</para>
51
+
</listitem>
52
+
</varlistentry>
36
53
</variablelist>
37
54
</para>
38
55
</refsect1>
...
...
@@ -50,51 +67,119 @@
50
67
&reftitle.examples;
51
68
<para>
52
69
<example>
53
-
<title><function>token_get_all</function> examples</title>
70
+
<title><function>token_get_all</function> example</title>
71
+
<programlisting role="php">
72
+
<![CDATA[
73
+
<?php
74
+
$tokens = token_get_all('<?php echo; ?>');
75
+

76
+
foreach ($tokens as $token) {
77
+
if (is_array($token)) {
78
+
echo "Line {$token[2]}: ", token_name($token[0]), " ('{$token[1]}')", PHP_EOL;
79
+
}
80
+
}
81
+
?>
82
+
]]>
83
+
</programlisting>
84
+
&example.outputs.similar;
85
+
<screen>
86
+
<![CDATA[
87
+
Line 1: T_OPEN_TAG ('<?php ')
88
+
Line 1: T_ECHO ('echo')
89
+
Line 1: T_WHITESPACE (' ')
90
+
Line 1: T_CLOSE_TAG ('?>')
91
+
]]>
92
+
</screen>
93
+
</example>
94
+
</para>
95
+
<para>
96
+
<example>
97
+
<title><function>token_get_all</function> incorrect usage example</title>
98
+
<programlisting role="php">
99
+
<![CDATA[
100
+
<?php
101
+
$tokens = token_get_all('/* comment */');
102
+

103
+
foreach ($tokens as $token) {
104
+
if (is_array($token)) {
105
+
echo "Line {$token[2]}: ", token_name($token[0]), " ('{$token[1]}')", PHP_EOL;
106
+
}
107
+
}
108
+
?>
109
+
]]>
110
+
</programlisting>
111
+
&example.outputs.similar;
112
+
<screen>
113
+
<![CDATA[
114
+
Line 1: T_INLINE_HTML ('/* comment */')
115
+
]]>
116
+
</screen>
117
+
</example>
118
+
Note in the previous example that the string is parsed as
119
+
<constant>T_INLINE_HTML</constant> rather than the expected
120
+
<constant>T_COMMENT</constant>. This is because no open tag was used in the
121
+
code provided. This would be equivalent to putting a comment outside of the
122
+
PHP tags in a normal file.
123
+
</para>
124
+
<para>
125
+
<example>
126
+
<title>
127
+
<function>token_get_all</function> on a class using a reserved word example
128
+
</title>
54
129
<programlisting role="php">
55
130
<![CDATA[
56
131
<?php
57
-
$tokens = token_get_all('<?php echo; ?>'); /* => array(
58
-
array(T_OPEN_TAG, '<?php'),
59
-
array(T_ECHO, 'echo'),
60
-
';',
61
-
array(T_CLOSE_TAG, '?>') ); */
62
132

63
-
/* Note in the following example that the string is parsed as T_INLINE_HTML
64
-
rather than the otherwise expected T_COMMENT (T_ML_COMMENT in PHP <5).
65
-
This is because no open/close tags were used in the "code" provided.
66
-
This would be equivalent to putting a comment outside of <?php ?> tags in a normal file. */
67
-
$tokens = token_get_all('/* comment */'); // => array(array(T_INLINE_HTML, '/* comment */'));
133
+
$source = <<<'code'
134
+
<?php
135
+

136
+
class A
137
+
{
138
+
const PUBLIC = 1;
139
+
}
140
+
code;
141
+

142
+
$tokens = token_get_all($source, TOKEN_PARSE);
143
+

144
+
foreach ($tokens as $token) {
145
+
if (is_array($token)) {
146
+
echo token_name($token[0]) , PHP_EOL;
147
+
}
148
+
}
68
149
?>
69
150
]]>
70
151
</programlisting>
152
+
&example.outputs.similar;
153
+
<screen>
154
+
<![CDATA[
155
+
T_OPEN_TAG
156
+
T_WHITESPACE
157
+
T_CLASS
158
+
T_WHITESPACE
159
+
T_STRING
160
+
T_CONST
161
+
T_WHITESPACE
162
+
T_STRING
163
+
T_LNUMBER
164
+
]]>
165
+
</screen>
71
166
</example>
167
+
Without the <constant>TOKEN_PARSE</constant> flag, the penultimate
168
+
token (<constant>T_STRING</constant>) would have been
169
+
<constant>T_PUBLIC</constant>.
72
170
</para>
73
171
</refsect1>
74
-
<refsect1 role="changelog">
75
-
&reftitle.changelog;
172
+

173
+
<refsect1 role="seealso">
174
+
&reftitle.seealso;
76
175
<para>
77
-
<informaltable>
78
-
<tgroup cols="2">
79
-
<thead>
80
-
<row>
81
-
<entry>&Version;</entry>
82
-
<entry>&Description;</entry>
83
-
</row>
84
-
</thead>
85
-
<tbody>
86
-
<row>
87
-
<entry>5.2.2</entry>
88
-
<entry>Line numbers are returned in element 2
89
-
</entry>
90
-
</row>
91
-
</tbody>
92
-
</tgroup>
93
-
</informaltable>
176
+
<simplelist>
177
+
<member><function>PhpToken::tokenize</function></member>
178
+
<member><function>token_name</function></member>
179
+
</simplelist>
94
180
</para>
95
181
</refsect1>
96
182
</refentry>
97
-

98
183
<!-- Keep this comment at the end of the file
99
184
Local variables:
100
185
mode: sgml
101
186