gpt4 book ai didi

PHP-REGEX - 多项选择类型

转载 作者:搜寻专家 更新时间:2023-10-31 21:32:16 24 4
gpt4 key购买 nike

我有这样一个字符串:

$str = "1. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway 2. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway"

我想要做的是得到这样的输出并将每个问题存储到 div:

1. What is love? 
a. Haddaway b. Haxxaway c. Hassaway d. Hannaway

2. What is love?
a. Haddaway b. Haxxaway c. Hassaway d. Hannaway

截至目前,我为正则表达式创建了这个编造的代码(仍然是新手,抱歉!)

$str = "1. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway 2. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway"
$repl = preg_replace('#(\d+:.*?)(?=\d+:|$) *#', "$1", $str);
echo "<div>$repl</div>";

输出是这样的,它存储在一个 div 中:

1. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway 2. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway

我对如何区分每个问题以及如何在每个选择上放置一个 div 感到困惑...任何人都可以就我该怎么做提供一些建议吗?

最佳答案

您可以像这样使用 preg_split:

$str = "1. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway 2. What is love? a. Haddaway b. Haxxaway c. Hassaway d. Hannaway";

print_r(preg_split('/(?=[a\d]+\.)/', $str, -1, PREG_SPLIT_NO_EMPTY));
Array
(
[0] => 1. What is love?
[1] => a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
[2] => 2. What is love?
[3] => a. Haddaway b. Haxxaway c. Hassaway d. Hannaway
)

(?=[a\d]+\.) 是一个前瞻,当其后跟数字或字母 a 加上一个点时拆分。

关于PHP-REGEX - 多项选择类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com