[DOC-CVS] [doc-en] master: strtr.xml Fix the confusing example, CS (#5754)
[email protected] (Mikhail Alferov via GitHub)
| Newsgroups | php.doc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: Mikhail Alferov (mmalferov)
Committer: GitHub (web-flow)
Pusher: kamil-tekiela
Date: 2026-08-11T01:42:58+01:00
Commit: https://github.com/php/doc-en/commit/1ed119182ad4629064b13301d0e427de47736bfe
Raw diff: https://github.com/php/doc-en/commit/1ed119182ad4629064b13301d0e427de47736bfe.diff
strtr.xml Fix the confusing example, CS (#5754)
Changed paths:
M reference/strings/functions/strtr.xml
Diff:
diff --git a/reference/strings/functions/strtr.xml b/reference/strings/functions/strtr.xml
index d746a319e4d2..1a3381612b03 100644
--- a/reference/strings/functions/strtr.xml
+++ b/reference/strings/functions/strtr.xml
@@ -114,15 +114,20 @@
<programlisting role="php">
<![CDATA[
<?php
-$addr = "The river å";
-// In this form, strtr() does byte-by-byte translation
-// Therefore, we are assuming a single-byte encoding here:
-$addr = strtr($addr, "äåö", "aao");
-echo $addr, PHP_EOL;
-?>
+$original = "He1Lo, w0rld!";
+
+// With three arguments, strtr() performs byte-by-byte translation.
+$result = strtr($original, "1L0", "llo");
+echo $result;
]]>
</programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+Hello, world!
+]]>
+ </screen>
</example>
</para>
<para>
@@ -136,9 +141,9 @@ echo $addr, PHP_EOL;
<programlisting role="php">
<![CDATA[
<?php
-$trans = array("h" => "-", "hello" => "hi", "hi" => "hello");
-echo strtr("hi all, I said hello", $trans);
-?>
+
+$replace_pairs = ["h" => "-", "hello" => "hi", "hi" => "hello"];
+echo strtr("hi all, I said hello", $replace_pairs);
]]>
</programlisting>
&example.outputs;
@@ -158,11 +163,22 @@ hello all, I said hi
<programlisting role="php">
<![CDATA[
<?php
-echo strtr("baab", "ab", "01"),"\n";
-$trans = array("ab" => "01");
-echo strtr("baab", $trans);
-?>
+// Single-byte characters
+$singlebyte_string = "baab";
+echo strtr($singlebyte_string, "ab", "01"), "\n"; // Byte-by-byte translation
+
+$replace_pairs = ["ab" => "01"];
+echo strtr($singlebyte_string, $replace_pairs), "\n"; // Substring translation
+
+// Multi-byte characters
+$multibyte_string = "Ä"; // In UTF-8, encoded as two bytes: "\xC3\x84"
+
+$result = strtr($multibyte_string, 'Ä', 'A');
+echo bin2hex($result), "\n"; // "4184", where "c3" becomes "41" ("A"), while "84" is kept and breaks UTF-8
+
+$replace_pairs = ['Ä' => 'A'];
+echo strtr($multibyte_string, $replace_pairs); // A correct multibyte substring translation
]]>
</programlisting>
&example.outputs;
@@ -170,6 +186,8 @@ echo strtr("baab", $trans);
<![CDATA[
1001
ba01
+4184
+A
]]>
</screen>
</example>