1

I created a generic function on a utility class that converts comma-separated strings to an array. The function has an optional parameter on what to trim but defaults to trimming spaces and quotes (single and double).

public function convertToArray($string, $trim = " '\"") {
    $split = array();

    if(!empty($string)) {
        $split = str_getcsv($string, ",");

        if(!empty($trim)) {
            $split = array_map(function($split) use ($trim) {
                return trim($split, $trim);
            }, $split);
        }
    }

    echo var_dump($string);
    echo var_dump($trim);
    echo var_dump($split);

    return $split;
}

On a separate file I'm calling it like this (I only want to trim spaces and keep the quotes intact):

$utility->convertToArray($keywords, " ");

The output for the 3 var_dump are as follows:

$string

string(21) "Finance, "Accounting""

$trim

string(1) " "

$split

array(2) {
  [0]=>
  string(7) "Finance"
  [1]=>
  string(10) "Accounting"
}

I expected to get string(12) ""Accounting"" but somehow the double quote is being trimmed. The function works well with single quotes though since I get string(12) "'Accounting'".

Additional Info

I'm using str_getcsv because the values could have commas within them and should not be split.

dokgu
  • 4,957
  • 3
  • 39
  • 77
  • 1
    Out of curiosity: why not use `explode`, instead of `$split = str_getcsv($string, ",");`? – yivi Jan 03 '17 at 17:03
  • 1
    Have you looked at what str_getcsv does? because csv's can have quotes in them which are not part of the data. – ontrack Jan 03 '17 at 17:04
  • `$split = str_getcsv($string, ",");` is what trims the double quotes by default – Mark Baker Jan 03 '17 at 17:08
  • Ok thanks for pointing that out! Initially I was using `explode` but had to change it to `str_getcsv` because of a reason I forgot. Eventually my requirements changed and I forgot to revert it back to `explode`. Here's the related question I had for it http://stackoverflow.com/questions/41286693/php-array-map-trim-parameters – dokgu Jan 03 '17 at 17:11
  • @yivi Parsing CSV is slightly more complicate. Commas can be valid values as well, not only separators. – Álvaro González Jan 03 '17 at 17:12
  • Álvaro, but he's not parsing csv. I think that's already established. – yivi Jan 03 '17 at 17:12
  • @ÁlvaroGonzález and @yivi There you go, that's the reason why I wanted to use `str_getcsv`. It's because I have values like this: `"Language and/or Literature, Fiction"` – dokgu Jan 03 '17 at 17:14
  • Then you stated your requirements incorrectly. Proposed solutions wont work, because everything is working as it should I guess. – yivi Jan 03 '17 at 17:15
  • 1
    @yivi You're right. In fact, we don't know the specs at all :) – Álvaro González Jan 03 '17 at 17:15
  • @ÁlvaroGonzález +1 – yivi Jan 03 '17 at 17:15

2 Answers2

1

It happens that str_getcsv() will already remove the quotes:

$s = 'hello,"world"';
var_dump(str_getcsv($s, ','));

# array(2) {
#   [0] => string(2) "Hello"
#   [1] => string(5) "world"
# }

You could use explode() instead:

$split = explode(',', $string);
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • Is there a way for me to keep using `str_getcsv` instead of `explode` because I have commas inside my values as well. – dokgu Jan 03 '17 at 17:17
  • @uom-pgregorio as far as I known, this would not be possible with this function. The problem here lies in the whole CSV specification. Honestly, my recommendation is if you need to deal with data where commas and quotes are important, use a better format. Of course it is possible to make this current way work, it is always possible, just depends on the effort you want to put it. And it feels like this would be too much effort to resolve the side-effect of a problem and not its cause – sidyll Jan 03 '17 at 17:53
  • how about regex? I'm open for it but I'm not good with regex. – dokgu Jan 03 '17 at 17:54
  • @uom-pgregorio Checkout this question: http://stackoverflow.com/questions/16476744/exploding-a-string-using-a-regular-expression – sidyll Jan 03 '17 at 18:03
1

Use explode instead str_getcsv.

I think that will fix your troubles.

str_getcsv will treat quotes as special "enclosure" delimiters, an remove them on its own.

explode is your friend.

yivi
  • 42,438
  • 18
  • 116
  • 138