将单行编号列表拆分为平面数组

发布于 2024-12-05 19:27:26 字数 413 浏览 0 评论 0原文

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

1. Comb. 2. Thumb. 3. Tomb (catacomb). 4. Womb. 5. Crumb. 6. Bomb. 7. Numb. 8. Aplomb. 9. Succumb.

有没有办法将其分解为如下所示:

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

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

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

So I'm trying to explode a string that has a list of answers.

1. Comb. 2. Thumb. 3. Tomb (catacomb). 4. Womb. 5. Crumb. 6. Bomb. 7. Numb. 8. Aplomb. 9. Succumb.

Is there a way to explode this to come out like the following:

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

The tricky thing is that I want to explode this string so that each answer can be separated after a number.

So, if the answer is 1 character, or 10 words, it will still split before each number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃→可爱长大的 2024-12-12 19:27:26

不,这不可能用explode()实现,但是你可以使用preg_split()

http://sandbox.phpcode.eu/g/4b041/1< /a>

<?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 />"; 
    } 
} 
?>

No, this is not possible with explode(), but you can use 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 />"; 
    } 
} 
?>
御守 2024-12-12 19:27:26

您可能想使用 preg_split() 相反。像这样的东西:

$answer = preg_split('/\d\./', $answerString);

You probably want to use preg_split() instead. Something like:

$answer = preg_split('/\d\./', $answerString);
够钟 2024-12-12 19:27:26
<?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 />"; 
    } 
?>
<?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 />"; 
    } 
?>
长伴 2024-12-12 19:27:26

在数字前的可选空格上拆分,后跟一个点。

$str = '1. Comb. 2. Thumb. 3. Tomb (catacomb). 4. Womb. 5. Crumb. 6. Bomb. 7. Numb. 8. Aplomb. 9. Succumb'; 

var_export(
    preg_split('/ *(?=\d+\.)/', $str, 0, PREG_SPLIT_NO_EMPTY)
);

输出:

array (
  0 => '1. Comb.',
  1 => '2. Thumb.',
  2 => '3. Tomb (catacomb).',
  3 => '4. Womb.',
  4 => '5. Crumb.',
  5 => '6. Bomb.',
  6 => '7. Numb.',
  7 => '8. Aplomb.',
  8 => '9. Succumb',
)

Split on the optional space before a number followed by a dot.

$str = '1. Comb. 2. Thumb. 3. Tomb (catacomb). 4. Womb. 5. Crumb. 6. Bomb. 7. Numb. 8. Aplomb. 9. Succumb'; 

var_export(
    preg_split('/ *(?=\d+\.)/', $str, 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => '1. Comb.',
  1 => '2. Thumb.',
  2 => '3. Tomb (catacomb).',
  3 => '4. Womb.',
  4 => '5. Crumb.',
  5 => '6. Bomb.',
  6 => '7. Numb.',
  7 => '8. Aplomb.',
  8 => '9. Succumb',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文