Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Hàm mb_stristr() trong PHP

Hàm mb_stristr() tìm xuất hiện đầu tiên của needle trong haystack, không phân biệt chữ hoa/thường. Trả về phần haystack từ vị trí tìm thấy (tùy before_needle). Trả về false nếu không tìm thấy.

Cú pháp

string|false mb_stristr(
    string $haystack,
    string $needle,
    bool $before_needle = false,
    string $encoding = null
)

Tham số

Tham số Mô tả
$haystack Chuỗi gốc.
$needle Chuỗi con cần tìm.
$before_needle true: trả về phần từ đầu đến trước needle (không bao gồm needle). false (mặc định): trả về từ needle đến cuối (bao gồm needle).
$encoding Encoding ký tự. Null = internal encoding.

Ví dụ 1: before_needle = false

<?php
$str = mb_stristr("Hello World", "or", false);
print_r($str);  // "orld"
?>

Ví dụ 2: before_needle = true

<?php
$str = mb_stristr("Hello World", "or", true);
print_r($str);  // "Hello W"
?>

Lưu ý

  • mb_stristr() tương tự stristr() nhưng hỗ trợ multibyte.
  • Cần extension mbstring.
  • Encoding set bởi mb_internal_encoding() hoặc tham số $encoding.

Hàm liên quan

  • mb_strstr() – Case-sensitive version.
  • mb_strrichr() – Tìm lần cuối (case-insensitive).
  • mb_strrchr() – Tìm lần cuối (case-sensitive).