ฟังก์ชันของ PHP ที่ใช้กับ Regular Expression
จากที่ผ่านมาเราได้ให้ความรู้เกี่ยวกับ Meta characters ไปแล้ว ในส่วนนี้เราจะพูดถึงฟังก์ชันของ PHP ที่นำเอา regex ไปใช้ซึ่งได้แก่
ฟังก์ัชั่นแบบ Perl Compatible Regular Expression (PCRE) กับ POSIX Extended Regular Expression
Perl compatible Regular Expression (PREC) ประกอบด้วย
POSIX Extended Regular Expression ประกอบด้วย
preg_match() ใช้สำหรับค้นหาข้อความ เปรียบเทียบข้อความกับ patthen โดยมี syntax คือ int preg_match (
string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] ) ตัวอย่างเช่น
<?php
$string = "http://docs.google.com/View?id=dvn0000_5ddbv0000";
if (preg_match('/^http:\/\/docs.google.com\/View\?id=([a-z0-9]{7})+_+([a-z0-9]{9})+$/', $string ))
{
echo "$string is Google Docs URL";
}
else
{
echo "$string is't Google Docs URL";
}
?>
ผลลัพธ์ที่ได้ก็จะเป็น http://docs.google.com/View?id=dvn0000_5ddbv0000 is Google Docs URL
ตัวอย่างการนำเอาข้อความที่ค้นหาเจอมาใช้งาน
<?php
$string = "YOUR IP IS 127.0.0.1";
if (preg_match('/(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/', $string, $match ))
{
echo $match[0];
}
else
{
echo "$string is't have IP Address";
}
?>
ผลลัพธ์ ที่ได้คือ 127.0.0.1
preg_replace() ใช้สำหรับค้นหาและแทนที่ข้อความ มี syntax คือ mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit=
-1 [, int &$count ]] ) preg_replace สามารถใช้ patthen ในรูปแบบของ array ได้ นั่นก็หมายถึง สามารถแทนที่ข้อความหลาย ๆ แบบ ด้วย ข้อความ หลาย ๆ แบบ ตัวอย่างเช่น
<?php
$patthen = array('/ll/', '/or/');
$strreplace = array('--', '--');
$string = "Hello World";
echo preg_replace($patthen, $strreplace, $string);
?>
ผลลัพธ์ที่ได้คือ He--o w--ld
ereg() ใช้ไว้สำหรับค้นหาข้อความ มี syntax คือ int ereg ( string $pattern , string $string [, array &$regs ] ) ถ้าคนพบรูปแบบที่กำหนดก็จะคืนค่า True ถ้าไม่พบหรือเกิดความผิดพลาด ก็จะคืนค่า False เราสามารถนำค่าที่ค้นหาเจอไปใช้ได้โดยการเพิ่ม parameter ตัวที่ 3 เข้ามา คือ &$regs และจะคืนค่าออกมาเป็น array เราจะมายกตัวอย่างให้ดูำักัน เช่น
<?php
$text = "Hello";
if (ereg("^[a-z]", $text)) {
echo "$text is Match.";
} else {
echo "$text is Not Match.";
}
?>
ผลลัพท์ทีได้จากการ ทำงานของฟังก์ชั่นข้างบนคือ จะพิมพ์ประโยค Hello is Match. ออกมาก
การนำเอาคำที่ค้นหาเจอไปใช้งาน สามารถทำไ้ด้ เช่น
<?php
$date = "01-01-2009";
if (ereg("([0-9]{2})-([0-9]{2})-([0-9]{4})", $date, $regs)) {
$year = $regs[3];
echo "this year is : $year";
} else {
echo "Date Format is Not Match !";
}
?>
จากตัวอย่างข้างบน จะได้ผลลัพธ์ คือ this year is : 2009 จะเห็นว่าเราได้เพิ่ม $regs เข้ามารับค่าของ array ที่เปรียบเทียบแล้วตรงกับ patthen ค่าที่มีอยู่ใน $regs จะเรียงตามกลุ่มของ patthen ที่เราระบุไว้ โดยจะมีข้อมูลอยู่ภายใน array ดังนี้
$regs[0] = "01-01-2009"
$regs[1] = "01"
$regs[2] = "01"
$regs[3] ="2009"
ereg_replace () ใช้่สำหรับค้นหาและแทนที่คำ ใน ข้อความ โดยใช้ patthen เป็นตัวระบุรูปแบบของข้อความ มี syntax คือ string ereg_replace
( string $pattern , string $replacement , string $string ) ถ้าทำการค้นหาเปรียบเทียบคำพบแล้ว ก็จะแทนที่ด้วยคำที่ระบุเข้าไปใน $replacement ตัวอย่างเช่น
<?php
$string = "Hello world";
echo ereg_replace("world", "sutee", $string);
// ผลลัพธ์ ที่ได้ออกมาคือ Hello sutee
?>
จากที่ผ่านมาเราได้ให้ความรู้เกี่ยวกับ Meta characters ไปแล้ว ในส่วนนี้เราจะพูดถึงฟังก์ชันของ PHP ที่นำเอา regex ไปใช้ซึ่งได้แก่
ฟังก์ัชั่นแบบ Perl Compatible Regular Expression (PCRE) กับ POSIX Extended Regular Expression
Perl compatible Regular Expression (PREC) ประกอบด้วย
- preg_grep
- preg_last_error
- preg_match_all
- preg_match
- preg_quote
- preg_replace_callback
- preg_replace
- preg_split
POSIX Extended Regular Expression ประกอบด้วย
- ereg_replace
- ereg
- eregi_replace
- eregi
- split
- spliti
- sql_regcase
preg_match() ใช้สำหรับค้นหาข้อความ เปรียบเทียบข้อความกับ patthen โดยมี syntax คือ int preg_match (
string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] ) ตัวอย่างเช่น
<?php
$string = "http://docs.google.com/View?id=dvn0000_5ddbv0000";
if (preg_match('/^http:\/\/docs.google.com\/View\?id=([a-z0-9]{7})+_+([a-z0-9]{9})+$/', $string ))
{
echo "$string is Google Docs URL";
}
else
{
echo "$string is't Google Docs URL";
}
?>
ผลลัพธ์ที่ได้ก็จะเป็น http://docs.google.com/View?id=dvn0000_5ddbv0000 is Google Docs URL
ตัวอย่างการนำเอาข้อความที่ค้นหาเจอมาใช้งาน
<?php
$string = "YOUR IP IS 127.0.0.1";
if (preg_match('/(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/', $string, $match ))
{
echo $match[0];
}
else
{
echo "$string is't have IP Address";
}
?>
ผลลัพธ์ ที่ได้คือ 127.0.0.1
preg_replace() ใช้สำหรับค้นหาและแทนที่ข้อความ มี syntax คือ mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit=
-1 [, int &$count ]] ) preg_replace สามารถใช้ patthen ในรูปแบบของ array ได้ นั่นก็หมายถึง สามารถแทนที่ข้อความหลาย ๆ แบบ ด้วย ข้อความ หลาย ๆ แบบ ตัวอย่างเช่น
<?php
$patthen = array('/ll/', '/or/');
$strreplace = array('--', '--');
$string = "Hello World";
echo preg_replace($patthen, $strreplace, $string);
?>
ผลลัพธ์ที่ได้คือ He--o w--ld
ereg() ใช้ไว้สำหรับค้นหาข้อความ มี syntax คือ int ereg ( string $pattern , string $string [, array &$regs ] ) ถ้าคนพบรูปแบบที่กำหนดก็จะคืนค่า True ถ้าไม่พบหรือเกิดความผิดพลาด ก็จะคืนค่า False เราสามารถนำค่าที่ค้นหาเจอไปใช้ได้โดยการเพิ่ม parameter ตัวที่ 3 เข้ามา คือ &$regs และจะคืนค่าออกมาเป็น array เราจะมายกตัวอย่างให้ดูำักัน เช่น
<?php
$text = "Hello";
if (ereg("^[a-z]", $text)) {
echo "$text is Match.";
} else {
echo "$text is Not Match.";
}
?>
ผลลัพท์ทีได้จากการ ทำงานของฟังก์ชั่นข้างบนคือ จะพิมพ์ประโยค Hello is Match. ออกมาก
การนำเอาคำที่ค้นหาเจอไปใช้งาน สามารถทำไ้ด้ เช่น
<?php
$date = "01-01-2009";
if (ereg("([0-9]{2})-([0-9]{2})-([0-9]{4})", $date, $regs)) {
$year = $regs[3];
echo "this year is : $year";
} else {
echo "Date Format is Not Match !";
}
?>
จากตัวอย่างข้างบน จะได้ผลลัพธ์ คือ this year is : 2009 จะเห็นว่าเราได้เพิ่ม $regs เข้ามารับค่าของ array ที่เปรียบเทียบแล้วตรงกับ patthen ค่าที่มีอยู่ใน $regs จะเรียงตามกลุ่มของ patthen ที่เราระบุไว้ โดยจะมีข้อมูลอยู่ภายใน array ดังนี้
$regs[0] = "01-01-2009"
$regs[1] = "01"
$regs[2] = "01"
$regs[3] ="2009"
ereg_replace () ใช้่สำหรับค้นหาและแทนที่คำ ใน ข้อความ โดยใช้ patthen เป็นตัวระบุรูปแบบของข้อความ มี syntax คือ string ereg_replace
( string $pattern , string $replacement , string $string ) ถ้าทำการค้นหาเปรียบเทียบคำพบแล้ว ก็จะแทนที่ด้วยคำที่ระบุเข้าไปใน $replacement ตัวอย่างเช่น
<?php
$string = "Hello world";
echo ereg_replace("world", "sutee", $string);
// ผลลัพธ์ ที่ได้ออกมาคือ Hello sutee
?>