Jump to content

[SOLVED] Split by commas


Orio

Recommended Posts

Hello,

 

Got myself into a problem here. Lets say I have the following string:

'1',0,'20506','Nov 17, 2007',444,'Hello, World',Array('1','2','3')

 

As you can see, the following string has commas both inside quotes (such as "Hello, Wrold" or "Nov 17, 2007"), but it also has commas that separate the different values. There are also values that don't have quotes around them. The "array" part is a hell of a problem itself...

How would I (by using preg_split I guess) separate the values one from another? The array I want to end up with is: (with or without the quotes)

'1'     0     '20506'     'Nov 17, 2007'     444     'Hello, World'      Array('1','2','3')

 

The problem is that I don't know how to split by commas that are outside of quotes (I mean after an even number of quotes).

 

Anyone got an idea here? Because I don't want to go char by char and count the number of quotes...

 

Thanks alot!

Orio.

Link to comment
Share on other sites

you can use this function found it somewhere in the php manual tellme if it works

 

<?php
function getCSVValues($string, $separator=",")
{
    $elements = explode($separator, $string);
    for ($i = 0; $i < count($elements); $i++) {
        $nquotes = substr_count($elements[$i], '"');
        if ($nquotes %2 == 1) {
            for ($j = $i+1; $j < count($elements); $j++) {
                if (substr_count($elements[$j], '"') > 0) {
                    // Put the quoted string's pieces back together again
                    array_splice($elements, $i, $j-$i+1,
                        implode($separator, array_slice($elements, $i, $j-$i+1)));
                    break;
                }
            }
        }
        if ($nquotes > 0) {
            // Remove first and last quotes, then merge pairs of quotes
            $qstr =& $elements[$i];
            $qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
            $qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
            $qstr = str_replace('""', '"', $qstr);
        }
    }
    return $elements;
}
?> 

Link to comment
Share on other sites

Ok I have trouble with the array part, I tried all day to fix it but I am a n00b when it comes to preg_replace()... So here's the deal:

I got in my string a part that looks like this:

 

...(more values here)...,some-value,Array('203','20','208','2'),'stats.php', ......

 

When using the piece of code rajivgonsalves has supplied, the contents of the array are being messed up. So what I thought of doing is to some how use preg_replace() to change:

Array('val1',...,'valN')
Into
val1|...|valN    OR    'val1'|....|'valN'

All that inside the string of course.

 

As I said, I am a n00b when it comes to regex, especially preg_replace(), so I'd appreciate it if someone could guide me here... I tried all kinds of stuff, but I didn't get to what I wanted here.

 

Note- The array may be empty, meaning it can be just "Array()".

 

Thanks alot,

Orio.

Link to comment
Share on other sites

lol I should have thought of that... The e modifier... Well I guess you learn something new every day :)

Here's the final version of it tho:

 

<?php
preg_replace("/Array\(([^\)]*)\)/ie", "str_replace(array(',',addslashes(\"'\")),array('|',''),\"$1\")", $val);
?>

 

Once again, thanks rajivgonsalves :)

 

*Solved*

 

Orio.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.