gpt4 book ai didi

php - 在第一个出现的子字符串上只分解一次字符串

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

preg_match() 给出一个匹配项。
preg_match_all() 返回所有匹配项。
preg_split() 返回所有拆分。

如何只在第一场比赛中拆分?

例子:

preg_split("~\n~", $string);

这就是我想要的:

array(
0 => 'first piece',
1 => 'the rest of the string
without any further splits'
)

最佳答案

只需将数组的 2 个部分的 $limit 设置为 2。感谢@BenJames 提到:

preg_split("~\n~", $string, 2);

我测试过,它工作正常。

极限参数:

If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or null means "no limit" and, as is standard across PHP, you can use null to skip to the flags parameter.

关于php - 在第一个出现的子字符串上只分解一次字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1668254/

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