GORAGOD.com
freelance, web developer, web designer, hosting, domain name
Home
Host & Domain
Portfolio
เกี่ยวกับ preg_match
Home
Forum
เกี่ยวกับ preg_match
บุ้ง
$match = array(a,b,c);
$date = 'test/a';
preg_match('/^test/('.implode('|', $match).')*?/u',$data,$match);
ผมเขียนแบบนี้ถูกรึป่าวครับ
บุ้ง
เหมือนจะถามผิดครับ preg_match('/^test/('.implode('|', $match).')*?/u',$data,$m);
บุ้ง
ได้แล้วครับ ถามเองงงเอง อิอิ
บุ้ง
แล้วถ้าทำแบบนีได้รึป่าวครับผมมี
$module_data ='test/cat/dog'
กับ $module_data ='testt/cat'
โดยที cat กับ dog นี้ทราบค่านะครับ
$cat = array('cat','catt')
ผมจะเขียน
preg_match('/^(test|testt)([\/]('.implode('|', $cat ).'))?$/u', $module_data, $modules)
ผมเข้าใจว่าการเขียน preg_match ด้านบน ครอบคลุมเงื่อนไข 'testt/cat'
ผมสามารถเขียน preg_match ได้อย่างไรครับถึงจะสามารถคลุมเงื่อนไข 'test/cat/dog' ได้
โดยที่สามารถใช้กับเงื่อนไข 'testt/cat' ได้
goragod
งง กะคำถามจริงๆ...
กรณีแรกที่ทำได้ คือ ใช้ preg_match 2 อัน อันแรกตรวจ คำสั่งนึง อันที่สองตรวจอีกคำสั่ง เช่น
preg_match('/test\/cat\/dog/') || preg_match('/testt\/cat/')
หรือ กรณีที่ 2 ใช้ OR หรือ | ใน preg
preg_match('/
(
(
test\/cat\/dog
)
|
(
/testt\/cat
)
)
/')
บุ้ง
แล้วจะเขียน .htaccess ยังไงให้เลือกว่าทุก path วิ่งมาหน้า index.php ยกเว้นบาง path อาจจะะเป็น /a อะไรพวกนี้นะครับ
บุ้ง
รบกวนช่วยดูหน่อยครับ มือใหม่หัดเขียน
if (preg_match('/^(propert_sale|propert_rent|holiday)([\/](search)(.*))?$/u', $module_data, $modules)){
//สำหรับเงื่อนไข propert_sale/search/?type=all&location=all&ref=&budget=
}
else if (preg_match('/^(propert_sale|propert_rent|holiday|land_sale|land_rent)([\/]('.implode('|', $prppert_type).')(.*))?$/u', $module_data, $modules)){
// สำหรับเงื่อน propert_sale/Villa
// และเงื่อนไข
propert_sale/Villa?currency=THB&page=2
}
else if (preg_match('/^(propert_sale|propert_rent|holiday|land_sale|land_rent)([\/]('.implode('|', $province_city).')(.*))?$/u', $module_data, $modules)){
// สำหรับเงื่อนไข propert_sale/Phuket-Mai Khao
// และเงื่อนไขpropert_sale/Phuket-Mai Khao?currency=TH&page=2
}
else if (preg_match('/^(propert_sale|propert_rent|holiday)([\/](.*)).html$/u', $module_data, $modules)){
// สำหรับเงื่อนไข propert_sale/property.html
}
บุ้ง
อีกนิดครับ ถ้าไม่ตรงเงือนไขด้านบนก็ให้เข้าถึงไฟล์ตรงๆไม่ต้องวิ่งมาที่ index.php
ที่ .htaccess ผมเขียนแบบนี้ครับ
RewriteRule ^(.*) index.php
goragod
ลองแล้วมันไก้ผลยังไงล่ะ....
ดูทั่วๆไปมันก็ถูก แต่ผมไม่รู้ว่าเงื่อนไขที่เป็นไปได้คืออะไรเลยตอบไม่ถูกว่าจะใช้ได้มั้ย ซึ่งเท่าที่ข้อมูลให้มามันก็น่าจะถูกนะ
ปล.
ไม่จำเป็นต้องใช้ [\/] สสามารถใช้ \/ แทน / ได้เลย
([\/](search)(.*))? อาจหมายถึง ไม่มีรายการนี้ก็ได้ ดังนั้นเงื่อนไขแรก มีแต่ propert_sale ก็ผ่านแล้ว
ยกตัวอย่าง ถ้าต้องการแค่ query string (propert_sale|propert_rent|holiday)\/search\/.*\?(.*) จาก propert_sale/search/?type=all&location=all&ref=&budget=
ความคิดเห็น
รายละเอียด
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^
$module_data ='test/cat/dog'
กับ $module_data ='testt/cat'
โดยที cat กับ dog นี้ทราบค่านะครับ
$cat = array('cat','catt')
ผมจะเขียน
preg_match('/^(test|testt)([\/]('.implode('|', $cat ).'))?$/u', $module_data, $modules)
ผมเข้าใจว่าการเขียน preg_match ด้านบน ครอบคลุมเงื่อนไข 'testt/cat'
ผมสามารถเขียน preg_match ได้อย่างไรครับถึงจะสามารถคลุมเงื่อนไข 'test/cat/dog' ได้
โดยที่สามารถใช้กับเงื่อนไข 'testt/cat' ได้
กรณีแรกที่ทำได้ คือ ใช้ preg_match 2 อัน อันแรกตรวจ คำสั่งนึง อันที่สองตรวจอีกคำสั่ง เช่น
preg_match('/test\/cat\/dog/') || preg_match('/testt\/cat/')
หรือ กรณีที่ 2 ใช้ OR หรือ | ใน preg
preg_match('/((test\/cat\/dog)|(/testt\/cat) ) /')
if (preg_match('/^(propert_sale|propert_rent|holiday)([\/](search)(.*))?$/u', $module_data, $modules)){
//สำหรับเงื่อนไข propert_sale/search/?type=all&location=all&ref=&budget=
}
else if (preg_match('/^(propert_sale|propert_rent|holiday|land_sale|land_rent)([\/]('.implode('|', $prppert_type).')(.*))?$/u', $module_data, $modules)){
// สำหรับเงื่อน propert_sale/Villa
// และเงื่อนไข propert_sale/Villa?currency=THB&page=2
}
else if (preg_match('/^(propert_sale|propert_rent|holiday|land_sale|land_rent)([\/]('.implode('|', $province_city).')(.*))?$/u', $module_data, $modules)){
// สำหรับเงื่อนไข propert_sale/Phuket-Mai Khao
// และเงื่อนไขpropert_sale/Phuket-Mai Khao?currency=TH&page=2
}
else if (preg_match('/^(propert_sale|propert_rent|holiday)([\/](.*)).html$/u', $module_data, $modules)){
// สำหรับเงื่อนไข propert_sale/property.html
}
ที่ .htaccess ผมเขียนแบบนี้ครับ
RewriteRule ^(.*) index.php
ดูทั่วๆไปมันก็ถูก แต่ผมไม่รู้ว่าเงื่อนไขที่เป็นไปได้คืออะไรเลยตอบไม่ถูกว่าจะใช้ได้มั้ย ซึ่งเท่าที่ข้อมูลให้มามันก็น่าจะถูกนะ
ปล.
ไม่จำเป็นต้องใช้ [\/] สสามารถใช้ \/ แทน / ได้เลย
([\/](search)(.*))? อาจหมายถึง ไม่มีรายการนี้ก็ได้ ดังนั้นเงื่อนไขแรก มีแต่ propert_sale ก็ผ่านแล้ว
ยกตัวอย่าง ถ้าต้องการแค่ query string (propert_sale|propert_rent|holiday)\/search\/.*\?(.*) จาก propert_sale/search/?type=all&location=all&ref=&budget=