PHP preg_quote()ฟังก์ชัน
ตัวอย่าง
ใช้ preg_quote() เพื่อใช้อักขระพิเศษในนิพจน์ทั่วไปอย่างปลอดภัย:
<?php
$search = preg_quote("://", "/");
$input = 'https://www.w3schools.com/';
$pattern = "/$search/";
if(preg_match($pattern, $input)) {
echo
"The input is a URL.";
} else {
echo "The input is not a URL.";
}
?>
ความหมายและการใช้งาน
ฟังก์ชันpreg_quote()
นี้จะเพิ่มแบ็กสแลชให้กับอักขระที่มีความหมายพิเศษในนิพจน์ทั่วไป เพื่อให้สามารถค้นหาอักขระตามตัวอักษรได้ ฟังก์ชันนี้มีประโยชน์เมื่อใช้การป้อนข้อมูลของผู้ใช้ในนิพจน์ทั่วไป
ไวยากรณ์
preg_quote(input, delimiter)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
input | Required. The string to be escaped |
delimiter | Optional. Defaults to null. This parameter expects a single character indicating which delimiter the regular expression will use. When provided, instances of this character in the input string will also be escaped with a backslash |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับสตริงที่มีอักขระพิเศษทั้งหมดยกเว้นด้วยแบ็กสแลช |
---|---|
รุ่น PHP: | 4+ |
บันทึกการเปลี่ยนแปลง: | PHP 7.3 - ตอนนี้อักขระ # ถือเป็นอักขระพิเศษและจะถูกหลบหนี PHP 5.3 - ตอนนี้อักขระ - ถือเป็นอักขระพิเศษและจะหนีได้ |
❮ การอ้างอิง PHP RegExp