reference/strings/functions/substr.xml
62126c55f1c6ed444043e7272c4f9e233818a44b
...
...
@@ -1,6 +1,6 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.substr">
3
+
<refentry xml:id="function.substr" xmlns="http://docbook.org/ns/docbook">
4
4
<refnamediv>
5
5
<refname>substr</refname>
6
6
<refpurpose>Return part of a string</refpurpose>
...
...
@@ -11,12 +11,12 @@
11
11
<methodsynopsis>
12
12
<type>string</type><methodname>substr</methodname>
13
13
<methodparam><type>string</type><parameter>string</parameter></methodparam>
14
-
<methodparam><type>int</type><parameter>start</parameter></methodparam>
15
-
<methodparam choice="opt"><type>int</type><parameter>length</parameter></methodparam>
14
+
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
15
+
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>length</parameter><initializer>&null;</initializer></methodparam>
16
16
</methodsynopsis>
17
17
<para>
18
18
Returns the portion of <parameter>string</parameter> specified by the
19
-
<parameter>start</parameter> and <parameter>length</parameter> parameters.
19
+
<parameter>offset</parameter> and <parameter>length</parameter> parameters.
20
20
</para>
21
21
</refsect1>
22
22

...
...
@@ -28,16 +28,16 @@
28
28
<term><parameter>string</parameter></term>
29
29
<listitem>
30
30
<para>
31
-
The input string. Must be one character or longer.
31
+
The input string.
32
32
</para>
33
33
</listitem>
34
34
</varlistentry>
35
35
<varlistentry>
36
-
<term><parameter>start</parameter></term>
36
+
<term><parameter>offset</parameter></term>
37
37
<listitem>
38
38
<para>
39
-
If <parameter>start</parameter> is non-negative, the returned string
40
-
will start at the <parameter>start</parameter>'th position in
39
+
If <parameter>offset</parameter> is non-negative, the returned string
40
+
will start at the <parameter>offset</parameter>'th position in
41
41
<parameter>string</parameter>, counting from zero. For instance,
42
42
in the string '<literal>abcdef</literal>', the character at
43
43
position <literal>0</literal> is '<literal>a</literal>', the
...
...
@@ -45,17 +45,17 @@
45
45
'<literal>c</literal>', and so forth.
46
46
</para>
47
47
<para>
48
-
If <parameter>start</parameter> is negative, the returned string
49
-
will start at the <parameter>start</parameter>'th character
48
+
If <parameter>offset</parameter> is negative, the returned string
49
+
will start at the <parameter>offset</parameter>'th character
50
50
from the end of <parameter>string</parameter>.
51
51
</para>
52
52
<para>
53
53
If <parameter>string</parameter> is less than
54
-
<parameter>start</parameter> characters long, &false; will be returned.
54
+
<parameter>offset</parameter> characters long, an empty string will be returned.
55
55
</para>
56
56
<para>
57
57
<example>
58
-
<title>Using a negative <parameter>start</parameter></title>
58
+
<title>Using a negative <parameter>offset</parameter></title>
59
59
<programlisting role="php">
60
60
<![CDATA[
61
61
<?php
...
...
@@ -75,24 +75,24 @@ $rest = substr("abcdef", -3, 1); // returns "d"
75
75
<para>
76
76
If <parameter>length</parameter> is given and is positive, the string
77
77
returned will contain at most <parameter>length</parameter> characters
78
-
beginning from <parameter>start</parameter> (depending on the length of
78
+
beginning from <parameter>offset</parameter> (depending on the length of
79
79
<parameter>string</parameter>).
80
80
</para>
81
81
<para>
82
82
If <parameter>length</parameter> is given and is negative, then that many
83
83
characters will be omitted from the end of <parameter>string</parameter>
84
84
(after the start position has been calculated when a
85
-
<parameter>start</parameter> is negative). If
86
-
<parameter>start</parameter> denotes the position of this truncation or
87
-
beyond, &false; will be returned.
85
+
<parameter>offset</parameter> is negative). If
86
+
<parameter>offset</parameter> denotes the position of this truncation or
87
+
beyond, an empty string will be returned.
88
88
</para>
89
89
<para>
90
90
If <parameter>length</parameter> is given and is <literal>0</literal>,
91
-
&false; or &null;, an empty string will be returned.
91
+
an empty string will be returned.
92
92
</para>
93
93
<para>
94
-
If <parameter>length</parameter> is omitted, the substring starting from
95
-
<parameter>start</parameter> until the end of the string will be
94
+
If <parameter>length</parameter> is omitted or &null;, the substring starting from
95
+
<parameter>offset</parameter> until the end of the string will be
96
96
returned.
97
97
</para>
98
98
<example>
...
...
@@ -102,7 +102,7 @@ $rest = substr("abcdef", -3, 1); // returns "d"
102
102
<?php
103
103
$rest = substr("abcdef", 0, -1); // returns "abcde"
104
104
$rest = substr("abcdef", 2, -1); // returns "cde"
105
-
$rest = substr("abcdef", 4, -4); // returns false
105
+
$rest = substr("abcdef", 4, -4); // returns ""; prior to PHP 8.0.0, false was returned
106
106
$rest = substr("abcdef", -3, -1); // returns "de"
107
107
?>
108
108
]]>
...
...
@@ -117,43 +117,39 @@ $rest = substr("abcdef", -3, -1); // returns "de"
117
117
<refsect1 role="returnvalues">
118
118
&reftitle.returnvalues;
119
119
<para>
120
-
Returns the extracted part of <parameter>string</parameter>; &return.falseforfailure;, or
120
+
Returns the extracted part of <parameter>string</parameter>, or
121
121
an empty string.
122
122
</para>
123
123
</refsect1>
124
124

125
125
<refsect1 role="changelog">
126
126
&reftitle.changelog;
127
-
<para>
128
-
<informaltable>
129
-
<tgroup cols="2">
130
-
<thead>
131
-
<row>
132
-
<entry>&Version;</entry>
133
-
<entry>&Description;</entry>
134
-
</row>
135
-
</thead>
136
-
<tbody>
137
-
<row>
138
-
<entry>7.0.0</entry>
139
-
<entry>
140
-
If <parameter>string</parameter> is equal to
141
-
<parameter>start</parameter> characters long, an empty string will be
142
-
returned. Prior to this version, &false; was returned in this case.
143
-
</entry>
144
-
</row>
145
-
<row>
146
-
<entry>5.2.2 - 5.2.6</entry>
147
-
<entry>
148
-
If the <parameter>start</parameter> parameter indicates the position of
149
-
a negative truncation or beyond, false is returned. Other versions get
150
-
the string from start.
151
-
</entry>
152
-
</row>
153
-
</tbody>
154
-
</tgroup>
155
-
</informaltable>
156
-
</para>
127
+
<informaltable>
128
+
<tgroup cols="2">
129
+
<thead>
130
+
<row>
131
+
<entry>&Version;</entry>
132
+
<entry>&Description;</entry>
133
+
</row>
134
+
</thead>
135
+
<tbody>
136
+
<row>
137
+
<entry>8.0.0</entry>
138
+
<entry>
139
+
<parameter>length</parameter> is nullable now.
140
+
When <parameter>length</parameter> is explicitly set to &null;,
141
+
the function returns a substring finishing at the end of the string, when it previously returned an empty string.
142
+
</entry>
143
+
</row>
144
+
<row>
145
+
<entry>8.0.0</entry>
146
+
<entry>
147
+
The function returns an empty string where it previously returned &false;.
148
+
</entry>
149
+
</row>
150
+
</tbody>
151
+
</tgroup>
152
+
</informaltable>
157
153
</refsect1>
158
154

159
155
<refsect1 role="examples">
...
...
@@ -165,6 +161,7 @@ $rest = substr("abcdef", -3, -1); // returns "de"
165
161
<![CDATA[
166
162
<?php
167
163
echo substr('abcdef', 1); // bcdef
164
+
echo substr("abcdef", 1, null); // bcdef; prior to PHP 8.0.0, empty string was returned
168
165
echo substr('abcdef', 1, 3); // bcd
169
166
echo substr('abcdef', 0, 4); // abcd
170
167
echo substr('abcdef', 0, 8); // abcdef
...
...
@@ -202,7 +199,7 @@ echo "7) ".var_export(substr(1.2e3, 0, 4), true).PHP_EOL;
202
199
?>
203
200
]]>
204
201
</programlisting>
205
-
&example.outputs.7;
202
+
&example.outputs;
206
203
<screen>
207
204
<![CDATA[
208
205
1) 'pe'
...
...
@@ -214,35 +211,33 @@ echo "7) ".var_export(substr(1.2e3, 0, 4), true).PHP_EOL;
214
211
7) '1200'
215
212
]]>
216
213
</screen>
217
-
&example.outputs.5;
218
-
<screen>
219
-
<![CDATA[
220
-
1) 'pe'
221
-
2) '54'
222
-
3) 'gr'
223
-
4) '1'
224
-
5) false
225
-
6) false
226
-
7) '1200'
227
-
]]>
228
-
</screen>
229
214
</example>
230
-
</para>
231
-
</refsect1>
232
-

233
-
<refsect1 role="errors">
234
-
&reftitle.errors;
235
-
<para>
236
-
Returns &false; on error.
237
-
<example>
238
-
<programlisting role="php">
215
+
<example>
216
+
<title>Invalid Character Range</title>
217
+
<para>
218
+
If an invalid character range is requested, <function>substr</function> returns
219
+
an empty string as of PHP 8.0.0; previously, &false; was returned instead.
220
+
</para>
221
+
<programlisting role="php">
239
222
<![CDATA[
240
223
<?php
241
-
var_dump(substr('a', 2)); // bool(false)
224
+
var_dump(substr('a', 2));
242
225
?>
243
226
]]>
244
-
</programlisting>
245
-
</example>
227
+
</programlisting>
228
+
&example.outputs.8;
229
+
<screen>
230
+
<![CDATA[
231
+
string(0) ""
232
+
]]>
233
+
</screen>
234
+
&example.outputs.7;
235
+
<screen>
236
+
<![CDATA[
237
+
bool(false)
238
+
]]>
239
+
</screen>
240
+
</example>
246
241
</para>
247
242
</refsect1>
248
243

...
...
@@ -262,7 +257,6 @@ var_dump(substr('a', 2)); // bool(false)
262
257
</refsect1>
263
258

264
259
</refentry>
265
-

266
260
<!-- Keep this comment at the end of the file
267
261
Local variables:
268
262
mode: sgml
269
263