เกี่ยวกับ preg_match

$match = array(a,b,c);

$date = 'test/a';

preg_match('/^test/('.implode('|', $match).')*?/u',$data,$match);



ผมเขียนแบบนี้ถูกรึป่าวครับ



09 ม.ค. 2555 8 3,273

เหมือนจะถามผิดครับ  preg_match('/^test/('.implode('|', $match).')*?/u',$data,$m);
#1

ได้แล้วครับ ถามเองงงเอง อิอิ
#2

แล้วถ้าทำแบบนีได้รึป่าวครับผมมี

$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' ได้
#3

งง กะคำถามจริงๆ...



กรณีแรกที่ทำได้ คือ ใช้ preg_match 2 อัน อันแรกตรวจ คำสั่งนึง อันที่สองตรวจอีกคำสั่ง เช่น



preg_match('/test\/cat\/dog/') || preg_match('/testt\/cat/')



หรือ กรณีที่ 2 ใช้ OR หรือ | ใน preg



preg_match('/((test\/cat\/dog)|(/testt\/cat) ) /')
#4

แล้วจะเขียน .htaccess ยังไงให้เลือกว่าทุก path วิ่งมาหน้า index.php ยกเว้นบาง path อาจจะะเป็น /a อะไรพวกนี้นะครับ

#5

รบกวนช่วยดูหน่อยครับ มือใหม่หัดเขียน



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

}
#6

อีกนิดครับ ถ้าไม่ตรงเงือนไขด้านบนก็ให้เข้าถึงไฟล์ตรงๆไม่ต้องวิ่งมาที่ index.php

ที่ .htaccess ผมเขียนแบบนี้ครับ

RewriteRule ^(.*) index.php

#7

ลองแล้วมันไก้ผลยังไงล่ะ....



ดูทั่วๆไปมันก็ถูก แต่ผมไม่รู้ว่าเงื่อนไขที่เป็นไปได้คืออะไรเลยตอบไม่ถูกว่าจะใช้ได้มั้ย ซึ่งเท่าที่ข้อมูลให้มามันก็น่าจะถูกนะ



ปล.



ไม่จำเป็นต้องใช้ [\/] สสามารถใช้ \/ แทน / ได้เลย



([\/](search)(.*))? อาจหมายถึง ไม่มีรายการนี้ก็ได้ ดังนั้นเงื่อนไขแรก มีแต่ propert_sale ก็ผ่านแล้ว



ยกตัวอย่าง ถ้าต้องการแค่ query string (propert_sale|propert_rent|holiday)\/search\/.*\?(.*) จาก propert_sale/search/?type=all&location=all&ref=&budget=
#8
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^