|
 |
|
สอบถามวิธีเขียนโค้ด php ให้แยกคำที่ต้องการออกมาจากประโยคยาวๆครับ |
|
 |
|
|
 |
 |
|
สวัสดีครับเพื่อนๆ ผมเป็นมือใหม่ php เลยอยากสอบถามอะไรนิดนึงครับ
คือว่าถ้าผมมี text ลักษณะนี้
สินค้ารายการที่ 1: Black Dumbbell
น้ำหนักข้างซ้าย: 5.00
น้ำหนักข้างขวา: 4.50
สินค้ารายการที่ 2: Silver Dumbbell
น้ำหนักข้างซ้าย: 10.00
น้ำหนักข้างขวา: 9.00
อยากสอบถามว่าถ้าผมจะเขียน php ให้แยก text ออกมาเป็นลักษณะนี้
Black Dumbbell (5.00, 4.50)
Silver Dumbbell (10.00, 9.00)
ต้องเขียนแบบไหนครับ?
พอดีผมได้ลอง
$unwanted_words = array('สินค้ารายการที่ 1:', ,'สินค้ารายการที่ 2:', 'น้ำหนักข้างซ้าย:', 'น้ำหนักข้างขวา:')
$word2replace = array('', '', '', '');
$output = str_replace($unwanted_words, $word2replace, $original_words) //original_words = text ข้างต้น
ทีนี้ผลที่ได้คือ
Black Dumbbell 5.00 4.50 Silver Dumbbell 10.00 9.00
น่ะครับ เลยไม่รู้ว่าจะจับมันไปทำอย่างไรต่อจึงจะได้ผลลัพธ์ตามต้องการ เลยต้องมารบกวนเพื่อนๆด้วยนะครับ -/\-
ปล. ตรงชื่อสินค้านี่มีความยาวสั้นไม่เท่ากันครับ บางตัวก็มีถึง 3 คำครับ เช่น Heavy Black Dumbbell
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2011-01-17 16:51:53 |
By :
Pete |
View :
845 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำได้หลายแบบตอนนี้ที่นึกออกคือใช้ฟังก์ชั่น explode
$test = explode(":",$string);
จะได้ค่าที่ถูกแยกเป็น array สองค่า ลองไป print_r ดูแล้วกัน
จากนั้นลองคลำต่อเอง
|
 |
 |
 |
 |
Date :
2011-01-17 17:50:41 |
By :
oxygenyoyo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|