บทช่วยสอนPHP

PHP HOME แนะนำ PHP การติดตั้ง PHP PHP ไวยากรณ์ ความคิดเห็น PHP ตัวแปร PHP PHP Echo / พิมพ์ ประเภทข้อมูล PHP PHP Strings หมายเลข PHP PHP คณิตศาสตร์ ค่าคงที่ PHP ตัวดำเนินการ PHP PHP If...Else...Elseif PHP Switch PHP Loops ฟังก์ชัน PHP PHP Arrays PHP Superglobals PHP RegEx

แบบฟอร์มPHP

การจัดการแบบฟอร์ม PHP การตรวจสอบแบบฟอร์ม PHP ต้องใช้แบบฟอร์ม PHP URL แบบฟอร์ม PHP/อีเมล แบบฟอร์ม PHP เสร็จสมบูรณ์

PHPขั้นสูง

วันที่และเวลา PHP รวม PHP การจัดการไฟล์ PHP ไฟล์ PHP เปิด/อ่าน สร้าง/เขียนไฟล์ PHP อัพโหลดไฟล์ PHP คุกกี้ PHP PHP Sessions ตัวกรอง PHP ตัวกรอง PHP ขั้นสูง ฟังก์ชันเรียกกลับของ PHP PHP JSON ข้อยกเว้น PHP

PHP OOP

PHP OOP คืออะไร คลาส PHP/วัตถุ ตัวสร้าง PHP PHP Destructor PHP Access Modifiers PHP Inheritance ค่าคงที่ PHP PHP Abstract Classes อินเทอร์เฟซ PHP PHP ลักษณะ PHP Static Methods PHP คุณสมบัติคงที่ PHP เนมสเปซ PHP Iterables

ฐานข้อมูลMySQL

ฐานข้อมูล MySQL MySQL Connect MySQL สร้าง DB MySQL สร้างตาราง MySQL แทรกข้อมูล MySQL รับ ID ล่าสุด MySQL แทรกหลายรายการ MySQL ที่เตรียมไว้ MySQL Select Data MySQL Where MySQL สั่งซื้อโดย MySQL ลบข้อมูล ข้อมูลอัพเดต MySQL MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - รับ PHP XML Expat PHP XML DOM

PHP - AJAX

บทนำ AJAX AJAX PHP ฐานข้อมูล AJAX AJAX XML AJAX ค้นหาสด AJAX โพล

ตัวอย่างPHP

ตัวอย่าง PHP PHP คอมไพเลอร์ แบบทดสอบ PHP แบบฝึกหัด PHP ใบรับรอง PHP

ข้อมูลอ้างอิงPHP

ภาพรวม PHP PHP Array ปฏิทิน PHP PHP วันที่ ไดเรกทอรี PHP ข้อผิดพลาด PHP ข้อยกเว้น PHP ระบบไฟล์ PHP ตัวกรอง PHP PHP FTP PHP JSON คีย์เวิร์ด PHP PHP Libxml PHP Mail PHP คณิตศาสตร์ PHP เบ็ดเตล็ด PHP MySQLi เครือข่าย PHP การควบคุมเอาต์พุต PHP PHP RegEx PHP SimpleXML PHP Stream PHP String การจัดการตัวแปร PHP PHP XML Parser PHP Zip เขตเวลา PHP

PHP preg_split()ฟังก์ชัน

❮ การอ้างอิง PHP RegExp

ตัวอย่าง

ใช้ preg_split() เพื่อแบ่งวันที่ออกเป็นส่วนประกอบ:

<?php
$date = "1970-01-01 00:00:00";
$pattern = "/[-\s:]/";
$components = preg_split($pattern, $date);
print_r($components);
?>

ความหมายและการใช้งาน

ฟังก์ชันpreg_split()จะแบ่งสตริงออกเป็นอาร์เรย์โดยใช้การจับคู่นิพจน์ทั่วไปเป็นตัวคั่น


ไวยากรณ์

preg_split(pattern, string, limit, flags)

ค่าพารามิเตอร์

Parameter Description
pattern Required. A regular expression determining what to use as a separator
string Required. The string that is being split
limit Optional. Defaults to -1, meaning unlimited. Limits the number of elements that the returned array can have. If the limit is reached before all of the separators have been found, the rest of the string will be put into the last element of the array
flags Optional. These flags provide options to change the returned array:
  • PREG_SPLIT_NO_EMPTY - Empty strings will be removed from the returned array.
  • PREG_SPLIT_DELIM_CAPTURE - If the regular expression contains a group wrapped in parentheses, matches of this group will be included in the returned array.
  • PREG_SPLIT_OFFSET_CAPTURE - Each element in the returned array will be an array with two element, where the first element is the substring and the second element is the position of the first character of the substring in the input string.

รายละเอียดทางเทคนิค

คืนมูลค่า: ส่งกลับอาร์เรย์ของสตริงย่อยที่แต่ละรายการสอดคล้องกับส่วนของสตริงอินพุตที่คั่นด้วยการจับคู่ของนิพจน์ทั่วไป
รุ่น PHP: 4+

ตัวอย่างเพิ่มเติม

ตัวอย่าง

การใช้แฟล็ก PREG_SPLIT_DELIM_CAPTURE:

<?php
$date = "1970-01-01 00:00:00";
$pattern = "/([-\s:])/";
$components = preg_split($pattern, $date, -1,
PREG_SPLIT_DELIM_CAPTURE);
print_r($components);
?>

ตัวอย่าง

ใช้การตั้งค่าสถานะ PREG_SPLIT_OFFSET_CAPTURE:

<?php
$date = "1970-01-01";
$pattern = "/-/";
$components = preg_split($pattern, $date, -1,
PREG_SPLIT_OFFSET_CAPTURE);
print_r($components);
?>

❮ การอ้างอิง PHP RegExp