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

Hàm explode() trong PHP

explode() tách chuỗi thành mảng dựa trên ký tự phân tách (delimiter).

Cú pháp

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

Tham số

  • separator: Ký tự ranh giới tách chuỗi (bắt buộc).
  • string: Chuỗi đầu vào (bắt buộc).
  • limit: Giới hạn số phần tử (tùy chọn). Dương: tối đa limit phần tử. Âm: loại bỏ |limit| phần tử cuối. 0: coi như 1.

Ví dụ

<?php
$str = "Hello,Welcome To Tutorials Point";
print_r(explode(" ", $str));        // 4 phần tử
print_r(explode(" ", $str, 3));     // tối đa 3 phần tử
?>

Kết quả

Array ( [0]=>Hello,Welcome [1]=>To [2]=>Tutorials [3]=>Point )
Array ( [0]=>Hello,Welcome [1]=>To [2]=>Tutorials Point )