gpt4 book ai didi

php - 如何拆分文本以匹配双引号加上尾随文本以点?

转载 作者:可可西里 更新时间:2023-11-01 13:27:26 27 4
gpt4 key购买 nike

如何获取双引号中有一个点必须分开的句子?

示例文档如下:

“Chess helps us overcome difficulties and sufferings,” said Unnikrishnan, taking my queen. “On a chess board you are fighting. as we are also fighting the hardships in our daily life.” he said.

我想得到这样的输出:

Array
(
[0] =>"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen.
[1] =>"On a chess board you are fighting. as we are also fighting the hardships in our daily life," he said.
)

我的代码仍然由点爆炸。

function sample($string)
{
$data=array();
$break=explode(".", $string);
array_push($data, $break);

print_r($data);
}

我仍然对拆分关于双引号和点的两个定界符感到困惑。因为在双引号里面有一个包含点分隔符的句子。

最佳答案

(*SKIP)(*FAIL) 的完美示例:

“[^“”]+”(*SKIP)(*FAIL)|\.\s*
# looks for strings in double quotes
# throws them away
# matches a dot literally, followed by whitespaces eventually


PHP 中:

$regex = '~“[^“”]+”(*SKIP)(*FAIL)|\.\s*~';
$parts = preg_split($regex, $your_string_here);

这产生

Array
(
[0] => “Chess helps us overcome difficulties and sufferings,” said Unnikrishnan, taking my queen
[1] => “On a chess board you are fighting. as we are also fighting the hardships in our daily life.”
)

参见 a demo on regex101.com以及a demo on ideone.com .

关于php - 如何拆分文本以匹配双引号加上尾随文本以点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44082644/

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