PHP preg_grep()ฟังก์ชัน
ตัวอย่าง
รับรายการจากอาร์เรย์ที่ขึ้นต้นด้วย "p":
<?php
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
ความหมายและการใช้งาน
ฟังก์ชันpreg_grep()
ส่งคืนอาร์เรย์ที่มีเฉพาะองค์ประกอบจากอินพุตที่ตรงกับรูปแบบที่กำหนด
ไวยากรณ์
preg_grep(pattern, input, flags)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. An array of strings |
flags | Optional. There is only one flag for this function. Passing the
constant PREG_GREP_INVERT will make the function return
only items that do not match the pattern. |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับอาร์เรย์ที่มีสตริงที่ตรงกับรูปแบบที่ให้มา |
---|---|
รุ่น PHP: | 4+ |
❮ การอ้างอิง PHP RegExp