<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Translate characters or replace substrings</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strtoupper.html">strtoupper</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr-compare.html">substr_compare</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.strtr" class="refentry"> <div class="refnamediv"> <h1 class="refname">strtr</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strtr</span> — <span class="dc-title">Translate characters or replace substrings</span></p> </div> <div class="refsect1 description" id="refsect1-function.strtr-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>strtr</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$from</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$to</code></span> )</div> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>strtr</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$replace_pairs</code></span> )</div> <p class="para rdfs-comment"> If given three arguments, this function returns a copy of <em><code class="parameter">str</code></em> where all occurrences of each (single-byte) character in <em><code class="parameter">from</code></em> have been translated to the corresponding character in <em><code class="parameter">to</code></em>, i.e., every occurrence of <em>$from[$n]</em> has been replaced with <em>$to[$n]</em>, where <em>$n</em> is a valid offset in both arguments. </p> <p class="para"> If <em><code class="parameter">from</code></em> and <em><code class="parameter">to</code></em> have different lengths, the extra characters in the longer of the two are ignored. The length of <em><code class="parameter">str</code></em> will be the same as the return value's. </p> <p class="para"> If given two arguments, the second should be an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> in the form <em>array('from' => 'to', ...)</em>. The return value is a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 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. </p> <p class="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 <em><code class="parameter">str</code></em>. However, this function will be the most efficient when all the keys have the same size. </p> </div> <div class="refsect1 parameters" id="refsect1-function.strtr-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">str</code></em></span> <dd> <p class="para"> The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> being translated. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">from</code></em></span> <dd> <p class="para"> The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> being translated to <em><code class="parameter">to</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">to</code></em></span> <dd> <p class="para"> The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> replacing <em><code class="parameter">from</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">replace_pairs</code></em></span> <dd> <p class="para"> The <em><code class="parameter">replace_pairs</code></em> parameter may be used instead of <em><code class="parameter">to</code></em> and <em><code class="parameter">from</code></em>, in which case it's an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> in the form <em>array('from' => 'to', ...)</em>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strtr-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the translated <span class="type"><a href="language.types.string.html" class="type string">string</a></span>. </p> <p class="para"> If <em><code class="parameter">replace_pairs</code></em> contains a key which is an empty <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (<em>""</em>), <strong><code>FALSE</code></strong> will be returned. If the <em><code class="parameter">str</code></em> is not a scalar then it is not typecasted into a string, instead a warning is raised and <strong><code>NULL</code></strong> is returned. </p> </div> <div class="refsect1 examples" id="refsect1-function.strtr-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4910"> <p><strong>Example #1 <span class="function"><strong>strtr()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">//In this form, strtr() does byte-by-byte translation<br />//Therefore, we are assuming a single-byte encoding here:<br /></span><span style="color: #0000BB">$addr </span><span style="color: #007700">= </span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #0000BB">$addr</span><span style="color: #007700">, </span><span style="color: #DD0000">"äåö"</span><span style="color: #007700">, </span><span style="color: #DD0000">"aao"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> The next example shows the behavior of <span class="function"><strong>strtr()</strong></span> when called with only two arguments. Note the preference of the replacements (<em>"h"</em> is not picked because there are longer matches) and how replaced text was not searched again. </p> <div class="example" id="example-4911"> <p><strong>Example #2 <span class="function"><strong>strtr()</strong></span> example with two arguments</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$trans </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"h" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"-"</span><span style="color: #007700">, </span><span style="color: #DD0000">"hello" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"hi"</span><span style="color: #007700">, </span><span style="color: #DD0000">"hi" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #DD0000">"hi all, I said hello"</span><span style="color: #007700">, </span><span style="color: #0000BB">$trans</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> hello all, I said hi </pre></div> </div> </div> <p class="para"> The two modes of behavior are substantially different. With three arguments, <span class="function"><strong>strtr()</strong></span> will replace bytes; with two, it may replace longer substrings. </p> <div class="example" id="example-4912"> <p><strong>Example #3 <span class="function"><strong>strtr()</strong></span> behavior comparison</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #DD0000">"baab"</span><span style="color: #007700">, </span><span style="color: #DD0000">"ab"</span><span style="color: #007700">, </span><span style="color: #DD0000">"01"</span><span style="color: #007700">),</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$trans </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"ab" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"01"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #DD0000">"baab"</span><span style="color: #007700">, </span><span style="color: #0000BB">$trans</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 1001 ba01 </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-function.strtr-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - Replace all occurrences of the search string with the replacement string</span></li> <li class="member"> <span class="function"><a href="function.preg-replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Perform a regular expression search and replace</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strtoupper.html">strtoupper</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr-compare.html">substr_compare</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>