2

所以我试图分解一个包含答案列表的字符串。

例如: 答案: 1. 梳子。2. 拇指。3. 坟墓(地下墓穴)。4. 子宫。5. 碎屑。6. 炸弹。7.麻木。8. 沉着。9. 屈服。

有没有办法把它爆炸出来,如下所示:

$answer = explode("something here", $answerString);
$answer[1] = 1. Comb.
$answer[2] = 2. Thumb.
$answer[3] = 3. 3. Tomb (catacomb).

棘手的是我想分解这个字符串,以便每个答案都可以在一个数字之后分开。

因此,如果答案是 1 个字符或 10 个单词,它仍然会在每个数字之后拆分。

谢谢。

4

3 回答 3

8

不,explode() 无法做到这一点,但您可以使用preg_split()

http://sandbox.phpcode.eu/g/4b041/1

<?php 
$str = '1. Comb. 2. Thumb. 3. Tomb (catacomb). 4. Womb. 5. Crumb. 6. Bomb. 7. Numb. 8. Aplomb. 9. Succumb'; 
$exploded = preg_split('/[0-9]+\./', $str); 
foreach($exploded as $index => $answer){ 
    if (!empty($answer)){ 
        echo $index.": ".$answer."<br />"; 
    } 
} 
?>
于 2011-09-21T19:15:47.473 回答
4

您可能想preg_split()改用。就像是:

$answer = preg_split('/\d\./', $answerString);
于 2011-09-21T19:16:31.620 回答
0
<?php 
$org_string = "1. Comb. 2. Thumb. 3. Tomb (catacomb). 4. Womb. 5. Crumb. 6. Bomb. 7. Numb. 8. Aplomb. 9. Succumb";
$new_string = str_replace("b. ", "b. ,", $org_string);
$final_string = str_replace("b). ", "b). ,", $new_string);
$exploded = explode(" ,",$final_string);
foreach($exploded as $answer){ 

        echo $answer."<br />"; 
    } 
?>
于 2016-12-01T12:00:30.540 回答