PHP strtr()ฟังก์ชัน
ตัวอย่าง
แทนที่อักขระ "ia" ในสตริงด้วย "eo":
<?php
echo strtr("Hilla Warld","ia","eo");
?>
ความหมายและการใช้งาน
ฟังก์ชัน strtr() แปลอักขระบางตัวในสตริง
หมายเหตุ:หากพารามิเตอร์ from และ to มีความยาวต่างกัน พารามิเตอร์ทั้งสองจะถูกจัดรูปแบบเป็นความยาวที่สั้นที่สุด
ไวยากรณ์
strtr(string,from,to)
หรือ
strtr(string,array)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
string | Required. Specifies the string to translate |
from | Required (unless array is used). Specifies what characters to change |
to | Required (unless array is used). Specifies what characters to change into |
array | Required (unless to and from is used). An array containing what to change from as key, and what to change to as value |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับสตริงที่แปลแล้ว หาก พารามิเตอร์ อาร์เรย์มีคีย์ซึ่งเป็นสตริงว่าง ("") พารามิเตอร์จะคืนค่า FALSE |
---|---|
รุ่น PHP: | 4+ |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
แทนที่สตริง "Hello world" ด้วย "Hi earth":
<?php
$arr = array("Hello" => "Hi", "world" => "earth");
echo strtr("Hello world",$arr);
?>
❮ การอ้างอิงสตริง PHP