/php/php-chunked-xhtml/function.strtr.html
HTML | 218 lines | 183 code | 35 blank | 0 comment | 0 complexity | e319ebc19a36809610a410fbd9091e1a MD5 | raw file
- <!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"><b>strtr</b></span>
- ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span>
- , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$from</tt></span>
- , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$to</tt></span>
- )</div>
- <div class="methodsynopsis dc-description">
- <span class="type">string</span> <span class="methodname"><b>strtr</b></span>
- ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span>
- , <span class="methodparam"><span class="type">array</span> <tt class="parameter">$replace_pairs</tt></span>
- )</div>
- <p class="para rdfs-comment">
- If given three arguments, this function returns a copy of
- <i><tt class="parameter">str</tt></i> where all occurrences of each (single-byte)
- character in <i><tt class="parameter">from</tt></i> have been translated to the
- corresponding character in <i><tt class="parameter">to</tt></i>, i.e., every
- occurrence of <i>$from[$n]</i> has been replaced with
- <i>$to[$n]</i>, where <i>$n</i> is a valid
- offset in both arguments.
- </p>
- <p class="para">
- If <i><tt class="parameter">from</tt></i> and <i><tt class="parameter">to</tt></i> have
- different lengths, the extra characters in the longer of the two
- are ignored. The length of <i><tt class="parameter">str</tt></i> 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 <i>array('from' => 'to', ...)</i>. 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; additionaly, the length of the return value may
- differ from that of <i><tt class="parameter">str</tt></i>.
- 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"><i><tt class="parameter">str</tt></i></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"><i><tt class="parameter">from</tt></i></span>
- <dd>
- <p class="para">
- The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> being translated to <i><tt class="parameter">to</tt></i>.
- </p>
- </dd>
- </dt>
- <dt>
- <span class="term"><i><tt class="parameter">to</tt></i></span>
- <dd>
- <p class="para">
- The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> replacing <i><tt class="parameter">from</tt></i>.
- </p>
- </dd>
- </dt>
- <dt>
- <span class="term"><i><tt class="parameter">replace_pairs</tt></i></span>
- <dd>
- <p class="para">
- The <i><tt class="parameter">replace_pairs</tt></i> parameter may be used instead of
- <i><tt class="parameter">to</tt></i> and <i><tt class="parameter">from</tt></i>, in which case it's an
- <span class="type"><a href="language.types.array.html" class="type array">array</a></span> in the form <i>array('from' => 'to', ...)</i>.
- </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 <i><tt class="parameter">replace_pairs</tt></i> contains a key which
- is an empty <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (<i>""</i>),
- <b><tt>FALSE</tt></b> will be 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-4157">
- <p><b>Example #1 <span class="function"><b>strtr()</b></span> example</b></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"><b>strtr()</b></span> when
- called with only two arguments. Note the preference of the replacements
- (<i>"h"</i> is not picked because there are longer matches)
- and how replaced text was not searched again.
- </p>
- <div class="example" id="example-4158">
- <p><b>Example #2 <span class="function"><b>strtr()</b></span> example with two arguments</b></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"><b>strtr()</b></span> will replace bytes; with two, it may replace
- longer substrings.
- </p>
- <div class="example" id="example-4159">
- <p><b>Example #3 <span class="function"><b>strtr()</b></span> behavior comparison</b></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>