php preg_split忽略特定字符串中的逗号

时间:2021-08-14 22:08:22

I need some help. What I want is to make ignore a comma in specific string. It is a comma seperated file csv, but the name have a comma, and I need to ignore that.

我需要一些帮助。我想要的是忽略特定字符串中的逗号。它是一个逗号分隔文件csv,但名称有一个逗号,我需要忽略它。

What I got is

我得到的是

<?php
    $pattern = '/([\\W,\\s]+Inc.])|[,]/';
    $subject = 'hypertext language, programming, Amazon, Inc., 100';
    $limit = -1;
    $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
    $result = preg_split ($pattern, $subject, $limit, $flags);
    ?>

Result is

$result (php code):

<?php
array (
  0 => 'hypertext language',
  1 => ' programming',
  2 => ' Amazon',
  3 => ' Inc.',
  4 => ' 100',
);
?>

And I want the result to be

我希望结果如此

$result (php code):

    <?php
    array (
      0 => 'hypertext language',
      1 => ' programming',
      2 => ' Amazon, Inc.',
      3 => ' 100',
    );
    ?>

Thanks for your help :)

谢谢你的帮助 :)

2 个解决方案

#1


3  

Note that [\W,\s] = \W since \W matches any char that is not a letter, digit or underscore. However, it seems you just want to split on a , that is not followed with space(s)*+Inc..

请注意,[\ W,\ s] = \ W,因为\ W匹配任何不是字母,数字或下划线的字符。但是,看起来你只想分开一个,那就是没有空格(*)* + Inc ..

You may use a negative lookahead to achieve this:

您可以使用负向前瞻来实现此目的:

/,(?!\s*Inc\.)/
  ^^^^^^^^^^^^

See the regex demo

请参阅正则表达式演示

The (?!\s*Inc\.) will fail any , match if there are 0+ whitespaces (\s*) followed with a sequence of literal characters Inc. after them.

(?!\ s * Inc \。)将失败,匹配如果有0+空格(\ s *)后面跟着一系列文字字符。

#2


1  

From your tutorial, if I pull the Amazon information as a CSV, I get the following format. Which you can then parse with one of Php's native functions. This shows you don't need to use explode or regex to handle this data. Use the right tool for the job:

从您的教程中,如果我将Amazon信息作为CSV提取,我将获得以下格式。然后,您可以使用Php的一个本机函数进行解析。这表明您不需要使用explode或regex来处理此数据。使用正确的工具:

<?php
$csv =<<<CSV
"amzn","Amazon.com, Inc.",765.56,"11/2/2016","4:00pm","-19.85 - -2.53%",10985
CSV;

$array = str_getcsv($csv);

var_dump($array);

Output:

array (size=7)
  0 => string 'amzn' (length=4)
  1 => string 'Amazon.com, Inc.' (length=16)
  2 => string '765.56' (length=6)
  3 => string '11/2/2016' (length=9)
  4 => string '4:00pm' (length=6)
  5 => string '-19.85 - -2.53%' (length=15)
  6 => string '10985' (length=5)

#1


3  

Note that [\W,\s] = \W since \W matches any char that is not a letter, digit or underscore. However, it seems you just want to split on a , that is not followed with space(s)*+Inc..

请注意,[\ W,\ s] = \ W,因为\ W匹配任何不是字母,数字或下划线的字符。但是,看起来你只想分开一个,那就是没有空格(*)* + Inc ..

You may use a negative lookahead to achieve this:

您可以使用负向前瞻来实现此目的:

/,(?!\s*Inc\.)/
  ^^^^^^^^^^^^

See the regex demo

请参阅正则表达式演示

The (?!\s*Inc\.) will fail any , match if there are 0+ whitespaces (\s*) followed with a sequence of literal characters Inc. after them.

(?!\ s * Inc \。)将失败,匹配如果有0+空格(\ s *)后面跟着一系列文字字符。

#2


1  

From your tutorial, if I pull the Amazon information as a CSV, I get the following format. Which you can then parse with one of Php's native functions. This shows you don't need to use explode or regex to handle this data. Use the right tool for the job:

从您的教程中,如果我将Amazon信息作为CSV提取,我将获得以下格式。然后,您可以使用Php的一个本机函数进行解析。这表明您不需要使用explode或regex来处理此数据。使用正确的工具:

<?php
$csv =<<<CSV
"amzn","Amazon.com, Inc.",765.56,"11/2/2016","4:00pm","-19.85 - -2.53%",10985
CSV;

$array = str_getcsv($csv);

var_dump($array);

Output:

array (size=7)
  0 => string 'amzn' (length=4)
  1 => string 'Amazon.com, Inc.' (length=16)
  2 => string '765.56' (length=6)
  3 => string '11/2/2016' (length=9)
  4 => string '4:00pm' (length=6)
  5 => string '-19.85 - -2.53%' (length=15)
  6 => string '10985' (length=5)