php-doc-en/reference/strings/functions/strtr.xml
2014-09-05 07:44:07 +00:00

206 lines
6.1 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.strtr" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>strtr</refname>
<refpurpose>Translate characters or replace substrings</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>string</type><methodname>strtr</methodname>
<methodparam><type>string</type><parameter>str</parameter></methodparam>
<methodparam><type>string</type><parameter>from</parameter></methodparam>
<methodparam><type>string</type><parameter>to</parameter></methodparam>
</methodsynopsis>
<methodsynopsis>
<type>string</type><methodname>strtr</methodname>
<methodparam><type>string</type><parameter>str</parameter></methodparam>
<methodparam><type>array</type><parameter>replace_pairs</parameter></methodparam>
</methodsynopsis>
<para>
If given three arguments, this function returns a copy of
<parameter>str</parameter> where all occurrences of each (single-byte)
character in <parameter>from</parameter> have been translated to the
corresponding character in <parameter>to</parameter>, i.e., every
occurrence of <literal>$from[$n]</literal> has been replaced with
<literal>$to[$n]</literal>, where <literal>$n</literal> is a valid
offset in both arguments.
</para>
<para>
If <parameter>from</parameter> and <parameter>to</parameter> have
different lengths, the extra characters in the longer of the two
are ignored. The length of <parameter>str</parameter> will be the same as
the return value's.
</para>
<para>
If given two arguments, the second should be an <type>array</type> in the
form <literal>array('from' => 'to', ...)</literal>. The return value is
a <type>string</type> where all the occurrences of the array keys have been
replaced by the corresponding values. The longest keys will be tried first.
Once a substring has been replaced, its new value will not be searched
again.
</para>
<para>
In this case, the keys and the values may have any length, provided that
there is no empty key; additionally, the length of the return value may
differ from that of <parameter>str</parameter>.
However, this function will be the most efficient when all the keys have the
same size.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>str</parameter></term>
<listitem>
<para>
The <type>string</type> being translated.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>from</parameter></term>
<listitem>
<para>
The <type>string</type> being translated to <parameter>to</parameter>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>to</parameter></term>
<listitem>
<para>
The <type>string</type> replacing <parameter>from</parameter>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>replace_pairs</parameter></term>
<listitem>
<para>
The <parameter>replace_pairs</parameter> parameter may be used instead of
<parameter>to</parameter> and <parameter>from</parameter>, in which case it's an
<type>array</type> in the form <literal>array('from' => 'to', ...)</literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the translated <type>string</type>.
</para>
<para>
If <parameter>replace_pairs</parameter> contains a key which
is an empty <type>string</type> (<literal>""</literal>),
&false; will be returned. If the <parameter>str</parameter> is not a scalar
then it is not typecasted into a string, instead a warning is raised and
&null; is returned.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>strtr</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
//In this form, strtr() does byte-by-byte translation
//Therefore, we are assuming a single-byte encoding here:
$addr = strtr($addr, "äåö", "aao");
?>
]]>
</programlisting>
</example>
</para>
<para>
The next example shows the behavior of <function>strtr</function> when
called with only two arguments. Note the preference of the replacements
(<literal>"h"</literal> is not picked because there are longer matches)
and how replaced text was not searched again.
</para>
<example>
<title><function>strtr</function> example with two arguments</title>
<programlisting role="php">
<![CDATA[
<?php
$trans = array("h" => "-", "hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
hello all, I said hi
]]>
</screen>
</example>
<para>
The two modes of behavior are substantially different. With three arguments,
<function>strtr</function> will replace bytes; with two, it may replace
longer substrings.
</para>
<example>
<title><function>strtr</function> behavior comparison</title>
<programlisting role="php">
<![CDATA[
<?php
echo strtr("baab", "ab", "01"),"\n";
$trans = array("ab" => "01");
echo strtr("baab", $trans);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
1001
ba01
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>str_replace</function></member>
<member><function>preg_replace</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
-->