Orio Posted November 17, 2007 Share Posted November 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 17, 2007 Share Posted November 17, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 17, 2007 Share Posted November 17, 2007 you can see it at http://www.php.net/manual/en/function.split.php Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Author Share Posted November 17, 2007 That works not bad, but there are a still a few bugs. I'll try to fix them on my own and if I'll have trouble I'll come and post again. Thanks alot rajivgonsalves Orio. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Author Share Posted November 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 17, 2007 Share Posted November 17, 2007 Cool I don't have my computer right now so cannot test the preg replace command but should be something like this preg_replace("/Array\(([^\)]+\)/ie","str_replace(',','|',$1)",$strString); let me know if you works Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Author Share Posted November 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 17, 2007 Share Posted November 17, 2007 no problem at all its my pleasure to be of a little help, I have learnt a lot of things from your post on the forums Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.