Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php preg_split ignore comma in specific string

Tags:

regex

php

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.

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 :)

like image 280
Ole Mariendal Avatar asked Nov 03 '16 09:11

Ole Mariendal


2 Answers

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..

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.

like image 115
Wiktor Stribiżew Avatar answered Nov 08 '22 13:11

Wiktor Stribiżew


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:

<?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)
like image 23
Progrock Avatar answered Nov 08 '22 15:11

Progrock