mirror of
https://github.com/php/doc-zh.git
synced 2026-04-23 16:18:14 +02:00
194 lines
6.0 KiB
XML
194 lines
6.0 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 45042fef652f1b4e904e809fcbfcf31f6c60670b Maintainer: daijie Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi -->
|
||
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.strtr" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||
<refnamediv>
|
||
<refname>strtr</refname>
|
||
<refpurpose>转换字符或替换字串</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>替代签名(不支持命名参数):</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>
|
||
如果指定三个参数,则该函数返回 <parameter>string</parameter> 的副本,<parameter>from</parameter>
|
||
中所有出现的每个(单字节)字符都已转换为 <parameter>to</parameter> 中的相应字符。例如,每次出现的
|
||
<literal>$from[$n]</literal> 都会替换为 <literal>$to[$n]</literal>,<literal>$n</literal>
|
||
是两个参数中的有效位移(offset)。
|
||
</para>
|
||
<para>
|
||
如果 <parameter>from</parameter> 与 <parameter>to</parameter>
|
||
长度不相等,那么忽略两者中较长的多余字符。<parameter>string</parameter> 的长度将与返回的长度一样。
|
||
</para>
|
||
<para>
|
||
如果指定两个参数,则第二个参数应该是 <literal>array('from' => 'to', ...)</literal> 形式的 <type>array</type>。返回值是
|
||
<type>string</type>,数组中所有所有出现的键都替换为相应的值。首先会尝试最长的键。一旦子字符串被替换,则新值将不会再次搜索。
|
||
</para>
|
||
<para>
|
||
在这种情况下,键和值可以是任意长度,前提是没有空值的键;另外,返回值的长度可以跟 <parameter>string</parameter>
|
||
的长度不同。但是,当所有键的长度相同时,函数最有效。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>string</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
待转换的 <type>string</type>。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>from</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
要转成 <parameter>to</parameter> 的 <type>string</type>。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>to</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
替换 <parameter>from</parameter> 的 <type>string</type>。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>replace_pairs</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
参数 <parameter>replace_pairs</parameter> 可以用来取代 <parameter>to</parameter> 和 <parameter>from</parameter>
|
||
,因为它是 <literal>array('from' => 'to', ...)</literal> 形式的 <type>array</type>。
|
||
</para>
|
||
<para>
|
||
如果 <parameter>replace_pairs</parameter> 包含空
|
||
<type>string</type>(<literal>""</literal>)键,将忽略该元素;自 PHP 8.0.0
|
||
起,这种情况会引发 <constant>E_WARNING</constant>。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
返回转换后的 <type>string</type>。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title><function>strtr</function> 示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$addr = "The river å";
|
||
|
||
// 在这种形式中,strtr() 进行逐字节转换
|
||
// 因此,假设这里是单字节编码:
|
||
$addr = strtr($addr, "äåö", "aao");
|
||
echo $addr, PHP_EOL;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
下面的示例展示了仅使用两个参数调用 <function>strtr</function> 的行为。注意优先替换(没有选择
|
||
<literal>"h"</literal>,因为有更长的匹配项)以及不再搜索已替换的文本。
|
||
</para>
|
||
<example>
|
||
<title>使用两个参数的 <function>strtr</function> 示例</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>
|
||
两种行为的模式本质上是不同的。使用三个参数,<function>strtr</function>
|
||
将按字节替换;使用两个参数,优先替换更长的子字符串,
|
||
</para>
|
||
<example>
|
||
<title><function>strtr</function> 行为比较</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
|
||
-->
|