gpt4 book ai didi

php - 在小于和大于之间拆分字符串

转载 作者:行者123 更新时间:2023-12-02 05:36:52 25 4
gpt4 key购买 nike

我需要拆分这种字符串以将电子邮件分隔为小于和大于 < >。我正在尝试使用下一个 regexpreg_split,但我不工作。

"email1@domain.com" <email1@domain.com>
News <news@e.domain.com>
Some Stuff <email-noreply@somestuff.com>

预期结果将是:

Array
(
[0] => "email1@domain.com"
[1] => email@email.com
)
Array
(
[0] => News
[1] => news@e.domain.com
)
Array
(
[0] => Some Stuff
[1] => email-noreply@somestuff.com
)

我现在使用的代码:

foreach ($emails as $email)
{
$pattern = '/<(.*?)>/';
$result = preg_split($pattern, $email);
print_r($result);
}

最佳答案

您可以使用一些可用于 preg_split 的标志: PREG_SPLIT_DELIM_CAPTURE PREG_SPLIT_NO_EMPTY

$emails = array('"email1@domain.com" <email1@domain.com>', 'News <news@e.domain.com>', 'Some Stuff <email-noreply@somestuff.com>');

foreach ($emails as $email)
{
$pattern = '/<(.*?)>/';
$result = preg_split($pattern, $email, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($result);
}

这会输出您所期望的:

Array
(
[0] => "email1@domain.com"
[1] => email1@domain.com
)
Array
(
[0] => News
[1] => news@e.domain.com
)
Array
(
[0] => Some Stuff
[1] => email-noreply@somestuff.com
)

关于php - 在小于和大于之间拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11558630/

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