PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/php/php-chunked-xhtml/function.strtr.html

http://github.com/matsubo/matsu-shell-setting
HTML | 218 lines | 183 code | 35 blank | 0 comment | 0 complexity | e319ebc19a36809610a410fbd9091e1a MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. <title>Translate characters or replace substrings</title>
  6. </head>
  7. <body><div class="manualnavbar" style="text-align: center;">
  8. <div class="prev" style="text-align: left; float: left;"><a href="function.strtoupper.html">strtoupper</a></div>
  9. <div class="next" style="text-align: right; float: right;"><a href="function.substr-compare.html">substr_compare</a></div>
  10. <div class="up"><a href="ref.strings.html">String Functions</a></div>
  11. <div class="home"><a href="index.html">PHP Manual</a></div>
  12. </div><hr /><div id="function.strtr" class="refentry">
  13. <div class="refnamediv">
  14. <h1 class="refname">strtr</h1>
  15. <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strtr</span> &mdash; <span class="dc-title">Translate characters or replace substrings</span></p>
  16. </div>
  17. <div class="refsect1 description" id="refsect1-function.strtr-description">
  18. <h3 class="title">Description</h3>
  19. <div class="methodsynopsis dc-description">
  20. <span class="type">string</span> <span class="methodname"><b>strtr</b></span>
  21. ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span>
  22. , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$from</tt></span>
  23. , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$to</tt></span>
  24. )</div>
  25. <div class="methodsynopsis dc-description">
  26. <span class="type">string</span> <span class="methodname"><b>strtr</b></span>
  27. ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span>
  28. , <span class="methodparam"><span class="type">array</span> <tt class="parameter">$replace_pairs</tt></span>
  29. )</div>
  30. <p class="para rdfs-comment">
  31. If given three arguments, this function returns a copy of
  32. <i><tt class="parameter">str</tt></i> where all occurrences of each (single-byte)
  33. character in <i><tt class="parameter">from</tt></i> have been translated to the
  34. corresponding character in <i><tt class="parameter">to</tt></i>, i.e., every
  35. occurrence of <i>$from[$n]</i> has been replaced with
  36. <i>$to[$n]</i>, where <i>$n</i> is a valid
  37. offset in both arguments.
  38. </p>
  39. <p class="para">
  40. If <i><tt class="parameter">from</tt></i> and <i><tt class="parameter">to</tt></i> have
  41. different lengths, the extra characters in the longer of the two
  42. are ignored. The length of <i><tt class="parameter">str</tt></i> will be the same as
  43. the return value&#039;s.
  44. </p>
  45. <p class="para">
  46. 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
  47. form <i>array(&#039;from&#039; =&gt; &#039;to&#039;, ...)</i>. The return value is
  48. 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
  49. replaced by the corresponding values. The longest keys will be tried first.
  50. Once a substring has been replaced, its new value will not be searched
  51. again.
  52. </p>
  53. <p class="para">
  54. In this case, the keys and the values may have any length, provided that
  55. there is no empty key; additionaly, the length of the return value may
  56. differ from that of <i><tt class="parameter">str</tt></i>.
  57. However, this function will be the most efficient when all the keys have the
  58. same size.
  59. </p>
  60. </div>
  61. <div class="refsect1 parameters" id="refsect1-function.strtr-parameters">
  62. <h3 class="title">Parameters</h3>
  63. <p class="para">
  64. <dl>
  65. <dt>
  66. <span class="term"><i><tt class="parameter">str</tt></i></span>
  67. <dd>
  68. <p class="para">
  69. The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> being translated.
  70. </p>
  71. </dd>
  72. </dt>
  73. <dt>
  74. <span class="term"><i><tt class="parameter">from</tt></i></span>
  75. <dd>
  76. <p class="para">
  77. 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>.
  78. </p>
  79. </dd>
  80. </dt>
  81. <dt>
  82. <span class="term"><i><tt class="parameter">to</tt></i></span>
  83. <dd>
  84. <p class="para">
  85. The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> replacing <i><tt class="parameter">from</tt></i>.
  86. </p>
  87. </dd>
  88. </dt>
  89. <dt>
  90. <span class="term"><i><tt class="parameter">replace_pairs</tt></i></span>
  91. <dd>
  92. <p class="para">
  93. The <i><tt class="parameter">replace_pairs</tt></i> parameter may be used instead of
  94. <i><tt class="parameter">to</tt></i> and <i><tt class="parameter">from</tt></i>, in which case it&#039;s an
  95. <span class="type"><a href="language.types.array.html" class="type array">array</a></span> in the form <i>array(&#039;from&#039; =&gt; &#039;to&#039;, ...)</i>.
  96. </p>
  97. </dd>
  98. </dt>
  99. </dl>
  100. </p>
  101. </div>
  102. <div class="refsect1 returnvalues" id="refsect1-function.strtr-returnvalues">
  103. <h3 class="title">Return Values</h3>
  104. <p class="para">
  105. Returns the translated <span class="type"><a href="language.types.string.html" class="type string">string</a></span>.
  106. </p>
  107. <p class="para">
  108. If <i><tt class="parameter">replace_pairs</tt></i> contains a key which
  109. is an empty <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (<i>&quot;&quot;</i>),
  110. <b><tt>FALSE</tt></b> will be returned.
  111. </p>
  112. </div>
  113. <div class="refsect1 examples" id="refsect1-function.strtr-examples">
  114. <h3 class="title">Examples</h3>
  115. <p class="para">
  116. <div class="example" id="example-4157">
  117. <p><b>Example #1 <span class="function"><b>strtr()</b></span> example</b></p>
  118. <div class="example-contents">
  119. <div class="phpcode"><code><span style="color: #000000">
  120. <span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//In&nbsp;this&nbsp;form,&nbsp;strtr()&nbsp;does&nbsp;byte-by-byte&nbsp;translation<br />//Therefore,&nbsp;we&nbsp;are&nbsp;assuming&nbsp;a&nbsp;single-byte&nbsp;encoding&nbsp;here:<br /></span><span style="color: #0000BB">$addr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #0000BB">$addr</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"дец"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"aao"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
  121. </span>
  122. </code></div>
  123. </div>
  124. </div>
  125. </p>
  126. <p class="para">
  127. The next example shows the behavior of <span class="function"><b>strtr()</b></span> when
  128. called with only two arguments. Note the preference of the replacements
  129. (<i>&quot;h&quot;</i> is not picked because there are longer matches)
  130. and how replaced text was not searched again.
  131. </p>
  132. <div class="example" id="example-4158">
  133. <p><b>Example #2 <span class="function"><b>strtr()</b></span> example with two arguments</b></p>
  134. <div class="example-contents">
  135. <div class="phpcode"><code><span style="color: #000000">
  136. <span style="color: #0000BB">&lt;?php<br />$trans&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"h"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"-"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"hello"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"hi"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"hi"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #DD0000">"hi&nbsp;all,&nbsp;I&nbsp;said&nbsp;hello"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$trans</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
  137. </span>
  138. </code></div>
  139. </div>
  140. <div class="example-contents"><p>The above example will output:</p></div>
  141. <div class="example-contents screen">
  142. <div class="cdata"><pre>
  143. hello all, I said hi
  144. </pre></div>
  145. </div>
  146. </div>
  147. <p class="para">
  148. The two modes of behavior are substantially different. With three arguments,
  149. <span class="function"><b>strtr()</b></span> will replace bytes; with two, it may replace
  150. longer substrings.
  151. </p>
  152. <div class="example" id="example-4159">
  153. <p><b>Example #3 <span class="function"><b>strtr()</b></span> behavior comparison</b></p>
  154. <div class="example-contents">
  155. <div class="phpcode"><code><span style="color: #000000">
  156. <span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #DD0000">"baab"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"ab"</span><span style="color: #007700">,&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"ab"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"01"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #DD0000">"baab"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$trans</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
  157. </span>
  158. </code></div>
  159. </div>
  160. <div class="example-contents"><p>The above example will output:</p></div>
  161. <div class="example-contents screen">
  162. <div class="cdata"><pre>
  163. 1001
  164. ba01
  165. </pre></div>
  166. </div>
  167. </div>
  168. </div>
  169. <div class="refsect1 seealso" id="refsect1-function.strtr-seealso">
  170. <h3 class="title">See Also</h3>
  171. <p class="para">
  172. <ul class="simplelist">
  173. <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>
  174. <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>
  175. </ul>
  176. </p>
  177. </div>
  178. </div><hr /><div class="manualnavbar" style="text-align: center;">
  179. <div class="prev" style="text-align: left; float: left;"><a href="function.strtoupper.html">strtoupper</a></div>
  180. <div class="next" style="text-align: right; float: right;"><a href="function.substr-compare.html">substr_compare</a></div>
  181. <div class="up"><a href="ref.strings.html">String Functions</a></div>
  182. <div class="home"><a href="index.html">PHP Manual</a></div>
  183. </div></body></html>