在电话号码后立即使用逗号分割字符串

发布于 2025-01-08 07:16:21 字数 506 浏览 0 评论 0原文

尝试分割字符串,但我不想“删除”我正在搜索的内容...

该字符串看起来像这样:

MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,

我想在电话号码后面分割字符串,但我不想删除号码。

现在,我有这个:

preg_split("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x", $string)

但是输出:

Array
(
    [0] => MDVB, 94010, 
    [1] => KHII, 94015, 
    [2] => POONHY, 94010, 
)

我认为 preg_split() 是要使用的东西。我还应该使用其他东西吗?

Trying to split a string, but I don't want to "remove" what I'm searching for...

The string Looks like this:

MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,

I want to split the string after the phone number, but I don't want to remove the number.

Right now, I have this:

preg_split("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x", $string)

But that outputs:

Array
(
    [0] => MDVB, 94010, 
    [1] => KHII, 94015, 
    [2] => POONHY, 94010, 
)

I thought preg_split() was the thing to use. Is there something else I should be using?

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

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

发布评论

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

评论(5

孤凫 2025-01-15 07:16:21

您应该使用 preg_match_all() 来提取字符串的部分。

<?php
$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";

$res = preg_match_all("/([A-Z]+,\s+\d+,\s+\(\d{3}\) \d{3}-\d{4})/",$string,$matches);
print_r($matches);
?>

输出:

Array
(
    [0] => Array
        (
            [0] => MDVB, 94010, (555) 555-5555
            [1] => KHII, 94015, (555) 555-5555
            [2] => POONHY, 94010, (555) 555-5555
        )

    [1] => Array
        (
            [0] => MDVB, 94010, (555) 555-5555
            [1] => KHII, 94015, (555) 555-5555
            [2] => POONHY, 94010, (555) 555-5555
        )

)

You should use preg_match_all() to extract portions of your string.

<?php
$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";

$res = preg_match_all("/([A-Z]+,\s+\d+,\s+\(\d{3}\) \d{3}-\d{4})/",$string,$matches);
print_r($matches);
?>

Outputs:

Array
(
    [0] => Array
        (
            [0] => MDVB, 94010, (555) 555-5555
            [1] => KHII, 94015, (555) 555-5555
            [2] => POONHY, 94010, (555) 555-5555
        )

    [1] => Array
        (
            [0] => MDVB, 94010, (555) 555-5555
            [1] => KHII, 94015, (555) 555-5555
            [2] => POONHY, 94010, (555) 555-5555
        )

)
美胚控场 2025-01-15 07:16:21

您可以使用 PREG_SPLIT_DELIM_CAPTURE 选项,这将导致捕获并返回分隔符模式中的括号表达式。

$parts = preg_split("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
                    $string,
                    null,
                    PREG_SPLIT_DELIM_CAPTURE);

结果数组:

array(7) {
  [0]=>
  string(13) "MDVB, 94010, "
  [1]=>
  string(3) "555"
  [2]=>
  string(15) ", KHII, 94015, "
  [3]=>
  string(3) "555"
  [4]=>
  string(17) ", POONHY, 94010, "
  [5]=>
  string(3) "555"
  [6]=>
  string(1) ","
}

我相信这是您正在寻找的行为。

You could use the PREG_SPLIT_DELIM_CAPTURE option which will cause the parenthesized expression in the delimiter pattern to be captured and returned as well.

$parts = preg_split("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
                    $string,
                    null,
                    PREG_SPLIT_DELIM_CAPTURE);

Resulting array:

array(7) {
  [0]=>
  string(13) "MDVB, 94010, "
  [1]=>
  string(3) "555"
  [2]=>
  string(15) ", KHII, 94015, "
  [3]=>
  string(3) "555"
  [4]=>
  string(17) ", POONHY, 94010, "
  [5]=>
  string(3) "555"
  [6]=>
  string(1) ","
}

I believe this is the behavior you were looking for.

终陌 2025-01-15 07:16:21
$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";
preg_match_all("/[A-Za-z]+, \d+, \(\d{3}\) \d{3}-\d{4}/", $string, $matches, PREG_SET_ORDER);

结果:

var_dump($matches);

array (
  0 => 
  array (
    0 => 'MDVB, 94010, (555) 555-5555',
  ),
  1 => 
  array (
    0 => 'KHII, 94015, (555) 555-5555',
  ),
  2 => 
  array (
    0 => 'POONHY, 94010, (555) 555-5555',
  ),
)
$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";
preg_match_all("/[A-Za-z]+, \d+, \(\d{3}\) \d{3}-\d{4}/", $string, $matches, PREG_SET_ORDER);

results:

var_dump($matches);

array (
  0 => 
  array (
    0 => 'MDVB, 94010, (555) 555-5555',
  ),
  1 => 
  array (
    0 => 'KHII, 94015, (555) 555-5555',
  ),
  2 => 
  array (
    0 => 'POONHY, 94010, (555) 555-5555',
  ),
)
魄砕の薆 2025-01-15 07:16:21

你最好像这样使用 preg_match_all :

preg_match_all("/(.*?(?:\(? (?:\d{3})? \)? [\-\s] )? \d{3}-\d{4})[^,]*(?:,|$)\s*/x",
               $string, $arr );
print_r($arr[1]);

输出:

Array
(
    [0] => MDVB, 94010, (555) 555-5555
    [1] => KHII, 94015, (555) 555-5555
    [2] => POONHY, 94010, (555) 555-5555
)

You should better use preg_match_all like this:

preg_match_all("/(.*?(?:\(? (?:\d{3})? \)? [\-\s] )? \d{3}-\d{4})[^,]*(?:,|$)\s*/x",
               $string, $arr );
print_r($arr[1]);

OUTPUT:

Array
(
    [0] => MDVB, 94010, (555) 555-5555
    [1] => KHII, 94015, (555) 555-5555
    [2] => POONHY, 94010, (555) 555-5555
)
隱形的亼 2025-01-15 07:16:21

preg_split() 不是一个可靠的验证工具。如果您需要验证,请使用 preg_match_all()。您的电话号码可能具有不同的前导格式这一事实与拆分过程无关 - 识别电话号码的结尾,使用 \K 忘记以前匹配的字符,然后匹配不需要的分隔逗号和可选空间。 演示

$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";

var_export(
    preg_split('/\d{3}-\d{4}\K, ?/', $string, 0, PREG_SPLIT_NO_EMPTY)
);

输出:

array (
  0 => 'MDVB, 94010, (555) 555-5555',
  1 => 'KHII, 94015, (555) 555-5555',
  2 => 'POONHY, 94010, (555) 555-5555',
)

preg_split() is not a reliable tool for validation. If you need validation, use preg_match_all(). The fact that your phone numbers may have varied leading formats is irrelevant to the splitting process -- identify the end of the phone number, use \K to forget previously matched characters, then match the unneeded delimiting comma and optional space. Demo

$string = "MDVB, 94010, (555) 555-5555, KHII, 94015, (555) 555-5555, POONHY, 94010, (555) 555-5555,";

var_export(
    preg_split('/\d{3}-\d{4}\K, ?/', $string, 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => 'MDVB, 94010, (555) 555-5555',
  1 => 'KHII, 94015, (555) 555-5555',
  2 => 'POONHY, 94010, (555) 555-5555',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文