Giới thiệu
Hàm str_ireplace() giống str_replace() nhưng KHÔNG phân biệt chữ hoa chữ thường (case-insensitive).
Cú pháp
str_ireplace(array|string $search, array|string $replace, string|array $subject, int &$count = null): string|array
Tham số
- search - Giá trị cần tìm (chuỗi hoặc mảng).
- replace - Giá trị thay thế.
- subject - Chuỗi hoặc mảng cần xử lý.
- count (tùy chọn) - Số lần thay thế (truyền tham chiếu).
Giá trị trả về
Trả về chuỗi hoặc mảng đã thay thế.
Ví dụ
Ví dụ 1: Thay thế không phân biệt hoa/thường
<?php
$str = "I am Amit";
$res = str_ireplace("amit", "David", $str);
echo $res;
?>
Kết quả:
I am David
Ví dụ 2: Trên mảng với count
<?php
$myArr = array("one", "two", "three");
print_r(str_ireplace("THREE", "four", $myArr, $c));
echo "
Number of Replacements = $c";
?>
Kết quả:
Array
(
[0] => one
[1] => two
[2] => four
)
Number of Replacements = 1