ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => หัวข้อเริ่มโดย: mahaboyd ใน 25 กรกฎาคม 2011, 17:53:57

ชื่อเรื่อง: [PHP] สอบถาม การตัดคำใน URL ครับ
โพสต์โดย: mahaboyd ใน 25 กรกฎาคม 2011, 17:53:57
คือ ถ้าผมมี URL แบบประมาณนี้
hxxp://www.amazon.com/Kindle-Wireless-Reader-Wifi-Graphite/dp/B002Y27P3M/ref=sr_1_3?s=electronics&ie=UTF8&qid=1311588426&sr=1-3
hxxp://www.amazon.com/HDMI-Cable-2M-6-Feet/dp/B0002L5R78/ref=sr_1_4?s=electronics&ie=UTF8&qid=1311588426&sr=1-4
hxxp://www.amazon.com/Kindle-Lighted-Leather-Display-Generation/dp/B003DZ165W/ref=sr_1_5?s=electronics&ie=UTF8&qid=1311588426&sr=1-5
hxxp://www.amazon.com/Transcend-Class-Flash-Memory-TS16GSDHC10E/dp/B003VNKNEQ/ref=sr_1_6?s=electronics&ie=UTF8&qid=1311588426&sr=1-6
hxxp://www.amazon.com/Kindle-Wireless-Reader-3G-Wifi-Graphite/dp/B002FQJT3Q/ref=sr_1_7?s=electronics&ie=UTF8&qid=1311588426&sr=1-7
hxxp://www.amazon.com/Kindle-Leather-Updated-Display-Generation/dp/B004JXVOD6/ref=sr_1_8?s=electronics&ie=UTF8&qid=1311588426&sr=1-8

แล้วผมจะตัดเอาเฉพาะ asin มันคืออันสีแดงหน่ะครับ... พอจะแนวไหมครับ...  :P
ชื่อเรื่อง: Re: [PHP] สอบถาม การตัดคำใน URL ครับ
โพสต์โดย: tatwizard ใน 25 กรกฎาคม 2011, 18:08:01
ดูเรื่อง regular expression ครับ ลองหาๆ ดูครับ มันจะมีตัวอย่างให้เป็นแนว เราเอาไปปรับไม่น่ายาก
ชื่อเรื่อง: Re: [PHP] สอบถาม การตัดคำใน URL ครับ
โพสต์โดย: mahaboyd ใน 25 กรกฎาคม 2011, 18:22:12
อ้างถึงจาก: tatwizard ใน 25 กรกฎาคม 2011, 18:08:01
ดูเรื่อง regular expression ครับ ลองหาๆ ดูครับ มันจะมีตัวอย่างให้เป็นแนว เราเอาไปปรับไม่น่ายาก

กำลังหาอยู่เลยครับ งงอยู่เพราะเพิ่งจะมาศึกษาจริงๆจังๆ 555+ เมื่อก่อนขี้เกียจไปหน่อย :'(
ชื่อเรื่อง: Re: [PHP] สอบถาม การตัดคำใน URL ครับ
โพสต์โดย: yoyokung ใน 25 กรกฎาคม 2011, 18:25:40
<?php
$txt
='xxp://www.amazon.com/Kindle-Wireless-Reader-Wifi-Graphite/dp/B002Y27P3M/ref=sr_1_3?s=electronics&ie=UTF8&qid=1311588426&sr=1-3
hxxp://www.amazon.com/HDMI-Cable-2M-6-Feet/dp/B0002L5R78/ref=sr_1_4?s=electronics&ie=UTF8&qid=1311588426&sr=1-4
hxxp://www.amazon.com/Kindle-Lighted-Leather-Display-Generation/dp/B003DZ165W/ref=sr_1_5?s=electronics&ie=UTF8&qid=1311588426&sr=1-5
hxxp://www.amazon.com/Transcend-Class-Flash-Memory-TS16GSDHC10E/dp/B003VNKNEQ/ref=sr_1_6?s=electronics&ie=UTF8&qid=1311588426&sr=1-6
hxxp://www.amazon.com/Kindle-Wireless-Reader-3G-Wifi-Graphite/dp/B002FQJT3Q/ref=sr_1_7?s=electronics&ie=UTF8&qid=1311588426&sr=1-7
hxxp://www.amazon.com/Kindle-Leather-Updated-Display-Generation/dp/B004JXVOD6/ref=sr_1_8?s=electronics&ie=UTF8&qid=1311588426&sr=1-8
'
;

  if (
$c=preg_match_all ("/(\\/)(dp)(\\/)((?:[a-z][a-z0-9_]*))(\\/)/is"$txt$matches))
  {
      
print_r($matches[4]);
  }
?>



แบบนี้ครับ print array ออกมาดู จะเห็นว่าได้ตามอย่างใจ ต่อไปก็เอาไปใช้ได้ตามสะดวกครับ
ชื่อเรื่อง: Re: [PHP] สอบถาม การตัดคำใน URL ครับ
โพสต์โดย: thenetxx ใน 25 กรกฎาคม 2011, 18:27:14
อิอิ ง่ายหน่อยก็ explode("/",$url);
ชื่อเรื่อง: Re: [PHP] สอบถาม การตัดคำใน URL ครับ
โพสต์โดย: mahaboyd ใน 25 กรกฎาคม 2011, 18:28:15
อ้างถึงจาก: yoyokung ใน 25 กรกฎาคม 2011, 18:25:40
<?php
$txt
='xxp://www.amazon.com/Kindle-Wireless-Reader-Wifi-Graphite/dp/B002Y27P3M/ref=sr_1_3?s=electronics&ie=UTF8&qid=1311588426&sr=1-3
hxxp://www.amazon.com/HDMI-Cable-2M-6-Feet/dp/B0002L5R78/ref=sr_1_4?s=electronics&ie=UTF8&qid=1311588426&sr=1-4
hxxp://www.amazon.com/Kindle-Lighted-Leather-Display-Generation/dp/B003DZ165W/ref=sr_1_5?s=electronics&ie=UTF8&qid=1311588426&sr=1-5
hxxp://www.amazon.com/Transcend-Class-Flash-Memory-TS16GSDHC10E/dp/B003VNKNEQ/ref=sr_1_6?s=electronics&ie=UTF8&qid=1311588426&sr=1-6
hxxp://www.amazon.com/Kindle-Wireless-Reader-3G-Wifi-Graphite/dp/B002FQJT3Q/ref=sr_1_7?s=electronics&ie=UTF8&qid=1311588426&sr=1-7
hxxp://www.amazon.com/Kindle-Leather-Updated-Display-Generation/dp/B004JXVOD6/ref=sr_1_8?s=electronics&ie=UTF8&qid=1311588426&sr=1-8
'
;

  if (
$c=preg_match_all ("/(\\/)(dp)(\\/)((?:[a-z][a-z0-9_]*))(\\/)/is"$txt$matches))
  {
      
print_r($matches[4]);
  }
?>



แบบนี้ครับ print array ออกมาดู จะเห็นว่าได้ตามอย่างใจ ต่อไปก็เอาไปใช้ได้ตามสะดวกครับ

โอ้ววว แจ่มเลยครับ... ขอบคุณมากครับได้ละ  :wanwan003:
ชื่อเรื่อง: Re: [PHP] สอบถาม การตัดคำใน URL ครับ
โพสต์โดย: xmen256k ใน 25 กรกฎาคม 2011, 18:30:59
สั่งแตกคำเข้า อาร์เรย์ครับ

โดยใช้ / แตกเอา