1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.strtr" xmlns="http://docbook.org/ns/docbook" 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>string</parameter></methodparam>
<methodparam><type>string</type><parameter>from</parameter></methodparam>
<methodparam><type>string</type><parameter>to</parameter></methodparam>
</methodsynopsis>
<simpara>Alternative signature (not supported with named arguments):</simpara>
<methodsynopsis>
<type>string</type><methodname>strtr</methodname>
<methodparam><type>string</type><parameter>string</parameter></methodparam>
<methodparam><type>array</type><parameter>replace_pairs</parameter></methodparam>
</methodsynopsis>
<para>
If given three arguments, this function returns a copy of
<parameter>string</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>string</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>string</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>string</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>
<para>
If <parameter>replace_pairs</parameter> contains a key which is an empty
<type>string</type> (<literal>""</literal>), the element is ignored;
as of PHP 8.0.0 <constant>E_WARNING</constant> is raised in this case.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the translated <type>string</type>.
</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
-->
|