PHP นิพจน์ทั่วไป
นิพจน์ทั่วไปคืออะไร?
นิพจน์ทั่วไปคือลำดับของอักขระที่สร้างรูปแบบการค้นหา เมื่อคุณค้นหาข้อมูลในข้อความ คุณสามารถใช้รูปแบบการค้นหานี้เพื่ออธิบายสิ่งที่คุณกำลังค้นหา
นิพจน์ทั่วไปอาจเป็นอักขระตัวเดียวหรือรูปแบบที่ซับซ้อนกว่าก็ได้
นิพจน์ทั่วไปสามารถใช้เพื่อดำเนินการค้นหาข้อความและการแทนที่ข้อความทุกประเภท
ไวยากรณ์
ใน PHP นิพจน์ทั่วไปคือสตริงที่ประกอบด้วยตัวคั่น รูปแบบ และตัวปรับแต่งเพิ่มเติม
$exp = "/w3schools/i";
ในตัวอย่างข้างต้น/
เป็นตัวคั่นw3schoolsคือรูปแบบที่กำลังค้นหา และi
เป็นตัวแก้ไขที่ทำให้การค้นหาไม่คำนึงถึงขนาดตัวพิมพ์
ตัวคั่นสามารถเป็นอักขระใดก็ได้ที่ไม่ใช่ตัวอักษร ตัวเลข แบ็กสแลช หรือช่องว่าง ตัวคั่นที่พบบ่อยที่สุดคือเครื่องหมายทับ (/) แต่เมื่อรูปแบบของคุณมีเครื่องหมายทับ คุณจะสะดวกในการเลือกตัวคั่นอื่นๆ เช่น # หรือ ~
ฟังก์ชันนิพจน์ทั่วไป
PHP มีฟังก์ชันหลากหลายที่ช่วยให้คุณใช้นิพจน์ทั่วไปได้ preg_match()
, preg_match_all()
และฟังก์ชัน
preg_replace()
เป็นฟังก์ชันที่ใช้บ่อยที่สุดบางส่วน:
Function | Description |
---|---|
preg_match() | Returns 1 if the pattern was found in the string and 0 if not |
preg_match_all() | Returns the number of times the pattern was found in the string, which may also be 0 |
preg_replace() | Returns a new string where matched patterns have been replaced with another string |
ใช้ preg_match()
ฟังก์ชันpreg_match()
จะบอกคุณว่าสตริงมีรูปแบบที่ตรงกันหรือไม่
ตัวอย่าง
ใช้นิพจน์ทั่วไปเพื่อค้นหา "w3schools" โดยไม่พิจารณาตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ในสตริง:
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
ใช้ preg_match_all()
ฟังก์ชันpreg_match_all()
นี้จะบอกคุณว่าพบรูปแบบในสตริงกี่รายการที่ตรงกัน
ตัวอย่าง
ใช้นิพจน์ทั่วไปเพื่อนับจำนวนครั้งที่ "ain" เกิดขึ้นในสตริงโดยไม่คำนึงถึงขนาดตัวพิมพ์:
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str); // Outputs 4
?>
ใช้ preg_replace()
ฟังก์ชันpreg_replace()
นี้จะแทนที่การจับคู่ทั้งหมดของรูปแบบในสตริงด้วยสตริงอื่น
ตัวอย่าง
ใช้นิพจน์ทั่วไปที่ไม่คำนึงถึงขนาดตัวพิมพ์เพื่อแทนที่ Microsoft ด้วย W3Schools ในสตริง:
<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo
preg_replace($pattern, "W3Schools", $str); // Outputs "Visit
W3Schools!"
?>
ตัวแก้ไขนิพจน์ทั่วไป
ตัวปรับแต่งสามารถเปลี่ยนวิธีดำเนินการค้นหาได้
Modifier | Description |
---|---|
i | Performs a case-insensitive search |
m | Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line) |
u | Enables correct matching of UTF-8 encoded patterns |
รูปแบบนิพจน์ทั่วไป
วงเล็บถูกใช้เพื่อค้นหาช่วงของอักขระ:
Expression | Description |
---|---|
[abc] | Find one character from the options between the brackets |
[^abc] | Find any character NOT between the brackets |
[0-9] | Find one character from the range 0 to 9 |
เมตาคาแรคเตอร์
Metacharacters เป็นอักขระที่มีความหมายพิเศษ:
Metacharacter | Description |
---|---|
| | Find a match for any one of the patterns separated by | as in: cat|dog|fish |
. | Find just one instance of any character |
^ | Finds a match as the beginning of a string as in: ^Hello |
$ | Finds a match at the end of the string as in: World$ |
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
ปริมาณ
ปริมาณกำหนดปริมาณ:
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
n{x} | Matches any string that contains a sequence of X n's |
n{x,y} | Matches any string that contains a sequence of X to Y n's |
n{x,} | Matches any string that contains a sequence of at least X n's |
หมายเหตุ:หากนิพจน์ของคุณต้องการค้นหาอักขระพิเศษตัวใดตัวหนึ่ง คุณสามารถใช้แบ็กสแลช ( \ ) เพื่อหลีกหนีจากอักขระเหล่านั้น ตัวอย่างเช่น หากต้องการค้นหาเครื่องหมายคำถามตั้งแต่หนึ่งเครื่องหมายขึ้นไป คุณสามารถใช้นิพจน์ต่อไปนี้: $pattern = '/\?+/';
การจัดกลุ่ม
คุณสามารถใช้วงเล็บ( )
เพื่อใช้ตัวระบุปริมาณกับรูปแบบทั้งหมดได้ นอกจากนี้ยังสามารถใช้เพื่อเลือกส่วนของลวดลายเพื่อใช้เป็นการจับคู่
ตัวอย่าง
ใช้การจัดกลุ่มเพื่อค้นหาคำว่า "กล้วย" โดยมองหาbaตามด้วยna สองกรณี :
<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo
preg_match($pattern, $str); // Outputs 1
?>
กรอกข้อมูลอ้างอิง RegExp ให้สมบูรณ์
สำหรับข้อมูลอ้างอิงที่สมบูรณ์ ไปที่Complete PHP Regular Expression Referenceของเรา
ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของฟังก์ชันนิพจน์ทั่วไปทั้งหมด