2002-04-15 00:12:54 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2004-07-03 12:13:06 +00:00
|
|
|
<!-- $Revision: 1.9 $ -->
|
2002-04-15 00:12:54 +00:00
|
|
|
<!-- splitted from ./en/functions/strings.xml, last change in rev 1.2 -->
|
|
|
|
<refentry id="function.substr">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>substr</refname>
|
|
|
|
<refpurpose>Return part of a string</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>string</type><methodname>substr</methodname>
|
|
|
|
<methodparam><type>string</type><parameter>string</parameter></methodparam>
|
|
|
|
<methodparam><type>int</type><parameter>start</parameter></methodparam>
|
|
|
|
<methodparam choice="opt"><type>int</type><parameter>length</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
2003-01-12 08:06:40 +00:00
|
|
|
<function>substr</function> returns the portion of <parameter>string</parameter>
|
2002-04-15 00:12:54 +00:00
|
|
|
specified by the <parameter>start</parameter> and
|
|
|
|
<parameter>length</parameter> parameters.
|
|
|
|
</para>
|
|
|
|
<para>
|
2002-07-13 08:44:43 +00:00
|
|
|
If <parameter>start</parameter> is non-negative, the returned string
|
2002-04-15 00:12:54 +00:00
|
|
|
will start at the <parameter>start</parameter>'th position in
|
|
|
|
<parameter>string</parameter>, counting from zero. For instance,
|
|
|
|
in the string '<literal>abcdef</literal>', the character at
|
|
|
|
position <literal>0</literal> is '<literal>a</literal>', the
|
|
|
|
character at position <literal>2</literal> is
|
|
|
|
'<literal>c</literal>', and so forth.
|
|
|
|
</para>
|
|
|
|
<example>
|
|
|
|
<title>Basic <function>substr</function> usage</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2002-07-08 20:49:45 +00:00
|
|
|
<?php
|
2002-04-15 00:12:54 +00:00
|
|
|
$rest = substr("abcdef", 1); // returns "bcdef"
|
|
|
|
$rest = substr("abcdef", 1, 3); // returns "bcd"
|
|
|
|
$rest = substr("abcdef", 0, 4); // returns "abcd"
|
|
|
|
$rest = substr("abcdef", 0, 8); // returns "abcdef"
|
2002-07-08 20:49:45 +00:00
|
|
|
|
|
|
|
// Accessing via curly braces is another option
|
|
|
|
$string = 'abcdef';
|
|
|
|
echo $string{0}; // returns a
|
|
|
|
echo $string{3}; // returns d
|
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<para>
|
|
|
|
If <parameter>start</parameter> is negative, the returned string
|
|
|
|
will start at the <parameter>start</parameter>'th character
|
2003-01-22 21:38:26 +00:00
|
|
|
from the end of <parameter>string</parameter>.
|
|
|
|
</para>
|
2002-04-15 00:12:54 +00:00
|
|
|
<example>
|
|
|
|
<title>Using a negative <parameter>start</parameter></title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2002-07-08 20:49:45 +00:00
|
|
|
<?php
|
2002-04-15 00:12:54 +00:00
|
|
|
$rest = substr("abcdef", -1); // returns "f"
|
|
|
|
$rest = substr("abcdef", -2); // returns "ef"
|
|
|
|
$rest = substr("abcdef", -3, 1); // returns "d"
|
2002-07-08 20:49:45 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<para>
|
|
|
|
If <parameter>length</parameter> is given and is positive, the string
|
|
|
|
returned will contain at most <parameter>length</parameter> characters
|
|
|
|
beginning from <parameter>start</parameter> (depending on the length of
|
2003-01-22 21:38:26 +00:00
|
|
|
<parameter>string</parameter>). If <parameter>string</parameter> is less
|
2004-07-03 12:13:06 +00:00
|
|
|
than or equal to <parameter>start</parameter> characters long, &false;
|
|
|
|
will be returned.
|
2002-04-15 00:12:54 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
If <parameter>length</parameter> is given and is negative, then that many
|
|
|
|
characters will be omitted from the end of <parameter>string</parameter>
|
|
|
|
(after the start position has been calculated when a
|
|
|
|
<parameter>start</parameter> is negative). If
|
|
|
|
<parameter>start</parameter> denotes a position beyond this truncation,
|
|
|
|
an empty string will be returned.
|
|
|
|
</para>
|
|
|
|
<example>
|
|
|
|
<title>Using a negative <parameter>length</parameter></title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2002-07-08 20:49:45 +00:00
|
|
|
<?php
|
2002-04-15 00:12:54 +00:00
|
|
|
$rest = substr("abcdef", 0, -1); // returns "abcde"
|
|
|
|
$rest = substr("abcdef", 2, -1); // returns "cde"
|
|
|
|
$rest = substr("abcdef", 4, -4); // returns ""
|
|
|
|
$rest = substr("abcdef", -3, -1); // returns "de"
|
2002-07-08 20:49:45 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<para>
|
2003-07-06 16:09:20 +00:00
|
|
|
See also <function>strrchr</function>,
|
2004-07-03 12:13:06 +00:00
|
|
|
<function>substr_replace</function>,
|
|
|
|
<function>ereg</function>,
|
|
|
|
<function>trim</function>,
|
|
|
|
<function>mb_substr</function> and
|
|
|
|
<function>wordwrap</function>.
|
2002-04-15 00:12:54 +00:00
|
|
|
</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:"../../../../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
|
|
|
|
-->
|