所以我想爆炸一个字符串,有一个答案列表。
答案: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个单词,它仍然会在每个数字之后分裂。
谢谢。
不,这是不可能的,但你可以使用 preg_split()
<?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 />";
}
}
?>
您可能想使用preg_split()
代替。比如:
$answer = preg_split('/'d'./', $answerString);
<?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 />";
}
?>