Phil
Phil

Reputation: 157

Split a CSV string only containing single-quoted values into a two-element array

I have a string that looks like this:

'word','another word','and a sentence','and more','etc etc'

I need to split this into two strings, divided by the second comma -- which shouldn't show in either of the sentences. What complicates things is that here can also be commas inside the various quotes parts of the string.

Upvotes: -1

Views: 124

Answers (5)

mickmackusa
mickmackusa

Reputation: 47764

For those who do not have a bias against regex, preg_split() can achieve the desired result in one concise call.

Match two repetitions of single-quoted substrings followed by a comma. \K is used to restart the fullstring match -- therefore consuming the unwanted comma between the two halves.

Code: (Demo)

$csv = "'word','another word','and a sentence','and more','etc etc'";
print_r(
    preg_split("/^(?:'[^']*'\K,){2}/", $csv)
);

Output:

Array
(
    [0] => 'word','another word'
    [1] => 'and a sentence','and more','etc etc'
)

Upvotes: 0

JOE LEE
JOE LEE

Reputation: 1058

$a="'word','another word','and a sentence','and more','etc etc'";

//preg_match('{(.*?,[^,]*),(.*)}', $a, $matches);
preg_match('{(.*?[^\\\\]\',.*?[^\\\\]\'),(.*)}', $a, $matches); //UPDATE 
print_r($matches);

DISPLAY:

    Array
(
    [0] => 'word','another word','and a sentence','and more','etc etc'
    [1] => 'word','another word'
    [2] => 'and a sentence','and more','etc etc'
)

Upvotes: 0

JOE LEE
JOE LEE

Reputation: 1058

commas in between quotes

$a="'word','another word','and a sentence','and more','etc etc'";

eval("\$arr =  array($a);");

$text1='';
$text2='';
foreach($arr AS $k=>$v){
    if($k<2){
        $text1.=$text1?',':'';
        $text1.="'{$v}'";
    }else{
        $text2.=$text2?',':'';
        $text2.="'{$v}'";
    }
}
echo $text1;
echo PHP_EOL;
echo $text2;

'word','another word'

'and a sentence','and more','etc etc'

Upvotes: 0

alwaysLearn
alwaysLearn

Reputation: 6950

Since you are saying that there can be commas in between quotes .. so preg_split can help you much better than explode

        <?php

                 $string = "'word','another word','and a sentence','and more','etc etc'";
                 $pattern = '%\',\'%';
                 $split = preg_split($pattern,$string);
                 $array1 = array();
                 $array2 = array();
                 foreach($split as $key=>$value)
                 {
                    $value  = trim($value,"'");
                    $value = "'{$value}'";

                    if(($key === 0) || ($key ===1))
                    {
                        $array1[] = $value;
                    }
                    else
                    {
                        $array2[] = $value; 
                    }
                 }

                echo $req_string1 = implode(',',$array1);
                echo "<br>";
                echo $req_string2 = implode(',',$array2);     


             ?>

Upvotes: 0

deceze
deceze

Reputation: 521995

This very much looks like CSV syntax, so:

$parsed  = str_getcsv($string, ',', "'");
$string1 = join(',', array_slice($parsed, 0, 2));
$string2 = join(',', array_slice($parsed, 2));

If your PHP version is below 5.3 and hence you don't have str_getcsv, you can replicate this using a virtual file handle to php://temp and fgetcsv.

Alternatively, depending on how difficult your syntax is, strtok can be used for a simple parser. Find the first ', then the next ', then a ,, then a ', then the next ' and you have the first part of the string...

Upvotes: 1

Related Questions