mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-19 14:22:43 +08:00
200 lines
9.9 KiB
HTML
200 lines
9.9 KiB
HTML
<!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>转换指定字符</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.strtr" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">strtr</h1>
|
||
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">strtr</span> — <span class="dc-title">转换指定字符</span></p>
|
||
|
||
</div>
|
||
|
||
<div class="refsect1 description" id="refsect1-function.strtr-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<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>
|
||
) : <span class="type">string</span></div>
|
||
|
||
<div class="methodsynopsis dc-description">
|
||
<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>
|
||
) : <span class="type">string</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
该函数返回 <code class="parameter">str</code> 的一个副本,并将在 <code class="parameter">from</code> 中指定的字符转换为 <code class="parameter">to</code> 中相应的字符。
|
||
比如, <em>$from[$n]</em>中每次的出现都会被替换为
|
||
<em>$to[$n]</em>,其中 <em>$n</em> 是两个参数都有效的位移(offset)。
|
||
</p>
|
||
<p class="para">
|
||
如果 <code class="parameter">from</code> 与 <code class="parameter">to</code> 长度不相等,那么多余的字符部分将被忽略。 <code class="parameter">str</code> 的长度将会和返回的值一样。
|
||
</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 <code class="parameter">str</code>.
|
||
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">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">str</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
待转换的<span class="type"><span class="type 字符串">字符串</span></span>。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">from</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
<span class="type"><span class="type 字符串">字符串</span></span>中与将要被转换的目的字符 <code class="parameter">to</code> 相对应的源字符。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">to</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
<span class="type"><span class="type 字符串">字符串</span></span>中与将要被转换的字符 <code class="parameter">from</code> 相对应的目的字符。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">replace_pairs</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
参数 <code class="parameter">replace_pairs</code> 可以用来取代 <code class="parameter">to</code> 和 <code class="parameter">from</code> 参数,因为它是以 <em>array('from' => 'to', ...)</em> 格式出现的数组。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.strtr-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
返回转换后的<span class="type"><span class="type 字符串">字符串</span></span>。
|
||
</p>
|
||
<p class="para">
|
||
如果 <code class="parameter">replace_pairs</code> 中包含一个空<span class="type"><span class="type 字符串">字符串</span></span>(<em>""</em>)键,那么将返回 <strong><code>FALSE</code></strong>。
|
||
If the <code class="parameter">str</code> 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">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-5988">
|
||
<p><strong>Example #1 <span class="function"><strong>strtr()</strong></span> 范例</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$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>
|
||
</pre></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-5989">
|
||
<p><strong>Example #2 使用两个参数的 <span class="function"><strong>strtr()</strong></span> 范例</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$trans </span><span style="color: #007700">= array(</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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>以上例程会输出:</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-5990">
|
||
<p><strong>Example #3 <span class="function"><strong>strtr()</strong></span> behavior comparison</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>以上例程会输出:</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">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="str_replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - 子字符串替换</span></li>
|
||
<li class="member"><span class="function"><a href="preg_replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a> - 执行一个正则表达式的搜索和替换</span></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |