Tutorial PHP

RUMAH PHP Pengenalan PHP Instal PHP Sintaks PHP Komentar PHP Variabel PHP PHP Gema / Cetak Tipe Data PHP String PHP Nomor PHP PHP Matematika Konstanta PHP Operator PHP PHP Jika...Lain...Elseif Beralih PHP PHP Loop Fungsi PHP Array PHP PHP Superglobal PHP RegEx

Formulir PHP

Penanganan Formulir PHP Validasi Formulir PHP Diperlukan Formulir PHP URL/Email Formulir PHP Formulir PHP Lengkap

PHP Lanjutan

Tanggal dan Waktu PHP PHP Termasuk Penanganan File PHP File PHP Buka/Baca Membuat/Menulis File PHP Unggah File PHP Cookie PHP Sesi PHP Filter PHP Filter PHP Tingkat Lanjut Fungsi Panggilan Balik PHP PHP JSON Pengecualian PHP

PHP OOP

PHP Apa itu OOP Kelas/Objek PHP Konstruktor PHP Penghancur PHP Pengubah Akses PHP Warisan PHP Konstanta PHP Kelas Abstrak PHP Antarmuka PHP Ciri-ciri PHP Metode Statis PHP Properti Statis PHP Ruang Nama PHP PHP Iterable

Database MySQL

Database MySQL Koneksi MySQL MySQL Buat DB MySQL Buat Tabel MySQL Sisipkan Data MySQL Dapatkan ID Terakhir MySQL Sisipkan Beberapa MySQL Disiapkan MySQL Pilih Data MySQL Dimana MySQL Dipesan Oleh MySQL Hapus Data Data Pembaruan MySQL Data Batas MySQL

PHP XML

PHP XML Parser PHP SimpleXML Parser PHP SimpleXML - Dapatkan PHP XML Ekspat PHP XML DOM

PHP - AJAX

Pengenalan AJAX AJAX PHP Basis Data AJAX AJAX XML Pencarian Langsung AJAX Jajak Pendapat AJAX

Contoh PHP

Contoh PHP Kompilator PHP Kuis PHP Latihan PHP Sertifikat PHP

Referensi PHP

Ikhtisar PHP Array PHP Kalender PHP Tanggal PHP Direktori PHP Kesalahan PHP Pengecualian PHP Sistem File PHP Filter PHP PHP FTP PHP JSON Kata Kunci PHP PHP Libxml Surat PHP PHP Matematika PHP Lain-lain PHP MySQLi Jaringan PHP Kontrol Keluaran PHP PHP RegEx PHP SimpleXML Aliran PHP String PHP Penanganan Variabel PHP PHP XML Parser PHP Zip Zona Waktu PHP

Fungsi PHP preg_match_all()

Referensi RegExp PHP

Contoh

Temukan semua kemunculan "ain" dalam sebuah string:

<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
  print_r($matches);
}
?>

Definisi dan Penggunaan

Fungsi preg_match_all()mengembalikan jumlah kecocokan pola yang ditemukan dalam string dan mengisi variabel dengan kecocokan yang ditemukan.


Sintaksis

preg_match_all(pattern, input, matches, flags, offset)

Nilai Parameter

Parameter Description
pattern Required. Contains a regular expression indicating what to search for
input Required. The string in which the search will be performed
matches Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found
flags Optional. A set of options that change how the matches array is structured.

One of the following structures may be selected:
  • PREG_PATTERN_ORDER - Default. Each element in the matches array is an array of matches from the same grouping in the regular expression, with index 0 corresponding to matches of the whole expression and the remaining indices for subpattern matches.
  • PREG_SET_ORDER - Each element in the matches array contains matches of all groupings for one of the found matches in the string.
Any number of the following options may be applied:
  • PREG_OFFSET_CAPTURE - When this option is enabled, each match, instead of being a string, will be an array where the first element is a substring containing the match and the second element is the position of the first character of the substring in the input.
  • PREG_UNMATCHED_AS_NULL - When this option is enabled, unmatched subpatterns will be returned as NULL instead of as an empty string.
offset Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter

Detail Teknis

Nilai Kembali: Mengembalikan jumlah kecocokan yang ditemukan atau salah jika terjadi kesalahan
Versi PHP: 4+
Catatan perubahan: PHP 7.2 - Menambahkan flag PREG_UNMATCHED_AS_NULL

PHP 5.4 - Parameter kecocokan menjadi opsional

PHP 5.3.6 - Fungsi mengembalikan false ketika offset lebih panjang dari panjang input

PHP 5.2.2 - Subpattern bernama dapat menggunakan (?'name' ) dan (? <name>) sintaks selain sebelumnya (?P<name>)

Lebih Banyak Contoh

Contoh

Gunakan PREG_PATTERN_ORDER untuk mengatur struktur larik yang cocok . Dalam contoh ini, setiap elemen dalam larik kecocokan memiliki semua kecocokan untuk salah satu pengelompokan ekspresi reguler.

<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER)) {
  print_r($matches);
}
?>

Referensi RegExp PHP