ฟังก์ชัน str_replace() ใช้เพื่อแทนที่สตริงด้วยสตริงอื่น
หมายเหตุ − ฟังก์ชันคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
ไวยากรณ์
str_replace(find, replace, str, count)
พารามิเตอร์
-
ค้นหา − ค่าที่จะค้นหา
-
เปลี่ยน − สตริงที่เราต้องการแทนที่ $find
-
str − สตริงที่จะค้นหา
-
นับ - จำนวนการเปลี่ยน
คืนสินค้า
ฟังก์ชัน str_replace() ส่งกลับสตริงหรืออาร์เรย์ที่มีค่าแทนที่
ตัวอย่าง
ต่อไปนี้เป็นตัวอย่าง −
<?php
$str = "I am Amit";
$res = str_replace("Amit", "David", $str);
echo $res;
?> ผลลัพธ์
I am David
ตัวอย่าง
ต่อไปนี้เป็นตัวอย่าง −
<?php
$myArr = array("one","two","three");
print_r(str_replace("three","four",$myArr,$c));
echo "<br>" . "Number of Replacements = $c";
?> ผลลัพธ์
Array ( [0] => one [1] => two [2] => four ) <br> Number of Replacements = 1