2011-12-14 57 views
0

嗨伙计我有一块文本,我想分成三部分,但我注意到这些部分没有什么共同点,我可以用作explode()函数的分隔符。字符串到3阵列与PHP

$description = '£5.97 for a Pillowheads Children’s Backpack worth £11.95 from Intelex Ltd - save 50% on your choice of five adorable designsNeed to know: Save 50% on an Intelex Ltd Pillowheads Children’s Backpack; Choose from five cute and cuddly designs; Postage and packaging is not included and costs £3.95; The perfect stocking filler this Christmas; What & why: Give your little ones an adorably cute, yet totally practical gift this Christmas with a cuddly creature that doubles as a backpack! For just £5.97 choose one of these gorgeous little Pillowheads Children’s Backpacks from Intelex Ltd. With a gorgeous selection of five fluffy animals to choose from, there's sure to be a pack to suit your little pride and joy! Designed with high-quality adjustable back straps and an inner lining for maximum support, these versatile bags will please even the most fussiest of toy lovers! choose one of the following designs: - Cosy Bee - Ladybird - Monkey - Pig - Puppy Each backpack is 43cm x 20xm in size and can be surface cleaned with a damp sponge. about Intelex Ltd Innovators of the muscle-relieving microwaveable herbal heat pack, Intelex Ltd have set the industry standard for quality and safety. Discover an inexpensive, fun and soothing way to relax in comfort at home with a range of products that combine practicality, comfort and style.By the way: Multiple vouchers may be purchased; This product is suitable for children aged three years ; and over; To order, please phone    0871 911 1770  , quoting your unique kgbdeals voucher code, stating which style you would like and provide your delivery details; Please allow 48 hours for your voucher code to be ; activated'; 

有没有什么办法可以分割这3块需要知道:什么,为什么,顺便说一句?

编辑:

$description = preg_split('/Need to know:| What & why:|By the way:/i', $product['text']['desc'], 3); 

好得到了,顺便位阵列,但“需要知道”和“什么&为什么”位在同一阵列门槛,任何人都可以在这方面帮助? :)

+0

我想你会需要找到另一种方式来获得你想要的信息。不知道你从哪里获取这些数据,但我认为你是从一个网站上获取这些数据的。通常当有这样的描述时,其他地方就会出现故障(如产品名称和价格)。 – Jemaclus 2011-12-14 19:27:11

+0

你的意思是哪三个部分? – TimWolla 2011-12-14 19:27:42

+0

此数据来自网站是其关联企业的广告客户Feed。描述是需要拆分的部分,其余的都很好:) @Jermaclus,谢谢:) – 2011-12-14 19:30:49

回答

3
preg_split('/Need to know:|What & why:|By the way:/i', ''£5.97 for a Pillowheads Children?s Backpack worth £11.95 from Intelex Ltd - save 50% on your choice of five adorable designsNeed to know: Save 50% on an Intelex Ltd Pillowheads Children?s Backpack; Choose from five cute and cuddly designs; Postage and packaging is not included and costs £3.95; The perfect stocking filler this Christmas; What & why: Give your little ones an adorably cute, yet totally practical gift this Christmas with a cuddly creature that doubles as a backpack! For just £5.97 choose one of these gorgeous little Pillowheads Children?s Backpacks from Intelex Ltd. With a gorgeous selection of five fluffy animals to choose from, there's sure to be a pack to suit your little pride and joy! Designed with high-quality adjustable back straps and an inner lining for maximum support, these versatile bags will please even the most fussiest of toy lovers! choose one of the following designs: - Cosy Bee - Ladybird - Monkey - Pig - Puppy Each backpack is 43cm x 20xm in size and can be surface cleaned with a damp sponge. about Intelex Ltd Innovators of the muscle-relieving microwaveable herbal heat pack, Intelex Ltd have set the industry standard for quality and safety. Discover an inexpensive, fun and soothing way to relax in comfort at home with a range of products that combine practicality, comfort and style.By the way: Multiple vouchers may be purchased; This product is suitable for children aged three years ; and over; To order, please phone 0871 911 1770 , quoting your unique kgbdeals voucher code, stating which style you would like and provide your delivery details; Please allow 48 hours for your voucher code to be ; activated'', null, 1); 

结果:

Array 
(
    [0] => '£5.97 for a Pillowheads Children?s Backpack worth £11.95 from Intelex Ltd - save 50% on your choice of five adorable designs 
    [1] => Save 50% on an Intelex Ltd Pillowheads Children?s Backpack; Choose from five cute and cuddly designs; Postage and packaging is not included and costs £3.95; The perfect stocking filler this Christmas; 
    [2] => Give your little ones an adorably cute, yet totally practical gift this Christmas with a cuddly creature that doubles as a backpack! For just £5.97 choose one of these gorgeous little Pillowheads Children?s Backpacks from Intelex Ltd. With a gorgeous selection of five fluffy animals to choose from, there's sure to be a pack to suit your little pride and joy! Designed with high-quality adjustable back straps and an inner lining for maximum support, these versatile bags will please even the most fussiest of toy lovers! choose one of the following designs: - Cosy Bee - Ladybird - Monkey - Pig - Puppy Each backpack is 43cm x 20xm in size and can be surface cleaned with a damp sponge. about Intelex Ltd Innovators of the muscle-relieving microwaveable herbal heat pack, Intelex Ltd have set the industry standard for quality and safety. Discover an inexpensive, fun and soothing way to relax in comfort at home with a range of products that combine practicality, comfort and style. 
    [3] => Multiple vouchers may be purchased; This product is suitable for children aged three years ; and over; To order, please phone 0871 911 1770 , quoting your unique kgbdeals voucher code, stating which style you would like and provide your delivery details; Please allow 48 hours for your voucher code to be ; activated' 
) 
-1

如果每个部分都有一定的长度,可以使用php strpos()函数将它们分开。否则,您可以在每个部分之后检查点和​​资本?希望这可以帮助。

0

由子只是把它分解:

$description = "£5.97 for a Pillowheads Children’s Backpack worth £11.95 from Intelex Ltd - save 50% on your choice of five adorable designsNeed to know: Save 50% on an Intelex Ltd Pillowheads Children’s Backpack; Choose from five cute and cuddly designs; Postage and packaging is not included and costs £3.95; The perfect stocking filler this Christmas; What & why: Give your little ones an adorably cute, yet totally practical gift this Christmas with a cuddly creature that doubles as a backpack! For just £5.97 choose one of these gorgeous little Pillowheads Children’s Backpacks from Intelex Ltd. With a gorgeous selection of five fluffy animals to choose from, there's sure to be a pack to suit your little pride and joy! Designed with high-quality adjustable back straps and an inner lining for maximum support, these versatile bags will please even the most fussiest of toy lovers! choose one of the following designs: - Cosy Bee - Ladybird - Monkey - Pig - Puppy Each backpack is 43cm x 20xm in size and can be surface cleaned with a damp sponge. about Intelex Ltd Innovators of the muscle-relieving microwaveable herbal heat pack, Intelex Ltd have set the industry standard for quality and safety. Discover an inexpensive, fun and soothing way to relax in comfort at home with a range of products that combine practicality, comfort and style.By the way: Multiple vouchers may be purchased; This product is suitable for children aged three years ; and over; To order, please phone    0871 911 1770  , quoting your unique kgbdeals voucher code, stating which style you would like and provide your delivery details; Please allow 48 hours for your voucher code to be ; activated"; 

$d_length = strlen($description); 
$part = (int) ($d_length/3); 

$p1 = substr($description, 0, $part); 
$p2 = substr($description, $part, $part); 
$p3 = substr($description, ($part*2)); 

echo $p1.PHP_EOL; 
echo $p2.PHP_EOL; 
echo $p3.PHP_EOL; 

演示:http://codepad.org/QIuklFW7

1

编辑:忽略此。我误读了帖子。因为它适用于任意字符串,所以将内容保留起来。


就我个人而言,我会分割空白。可能有更好的地方,但不是没有关于应用程序的更多细节。

这里有一个快速的肮脏的方法(使用wordwrap功能):

// Number of desired chunks 
$chunks = 3; 
$data = "LONG CHUNK OF TEXT HERE"; 

// Prep the data by removing newlines. 
$data = str_replace("\n", '', $data); 

// Use the built in wordwrap function to split to size 
$data = wordwrap($data, ceil(strlen($data)/$chunks)); 

// Split on the newly inserted newlines 
$parts = explode("\n", $data); 

// $parts is now an array with $chunks number of parts, all about the same length. 
0

还不如优雅的正则表达式,但:

$start2 = strpos($description, "What & why:"); 
$start3 = strpos($description, "By the way:"); 
$part1 = substr($description, 0, $start2); 
$part2 = substr($description, $start2, $start3 - $start2); 
$part3 = substr($description, $start3); 

编辑:这样做是为了排除什么&为什么/顺便说一句:

$start2 = strpos($description, "What & why:") + strlen("What & why:"); 
$start3 = strpos($description, "By the way:") + strlen("By the way:"); 
$end1 = $start2 - strlen("What & why:"); 
$end2 = $start3 - strlen("By the way:") - $start2; 

$part1 = substr($description, 0, $end1); 
$part2 = substr($description, $start2, $end2); 
$part3 = substr($description, $start3);