timbrown Posted October 30, 2007 Share Posted October 30, 2007 Hello, This has been causing me grief for hours! I have a string like this - "'something1','something2','1,2,3'" I want to split or explode at the commas, without exploding the 1,2,3 part. The values are user input so in fact could contain any characters. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/ Share on other sites More sharing options...
atlanta Posted October 30, 2007 Share Posted October 30, 2007 explode on ',' <? $test = "'something1','something2','1,2,3'"; $test2 = explode("','",$test); echo $test2[0]; echo $test2[1]; echo $test2[2]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/#findComment-381257 Share on other sites More sharing options...
timbrown Posted October 30, 2007 Author Share Posted October 30, 2007 Thanks for the reply. I think that with your suggestion if the user included the characters ',' in the string then it would cause an undesired split. I was thinking there might be a way of escaping commas like this - something1,something2,1\,2\,3 but I cant figure out how to split only on the non-escaped commas. by the way, the string is coming from GROUP_CONCAT in mysql. Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/#findComment-381265 Share on other sites More sharing options...
GingerRobot Posted October 30, 2007 Share Posted October 30, 2007 Erm, if you're pulling data from a mysql table, which you're concatenating with the mysql query, only to then explode, what's the point? Why not cycle through the rows and do what you want to do with the data. Secondly, as you mentioned, the proposed solution would run into troubles if there was a single quote contained inside the string - if this is an issue, then what about if the user places a comma inside the string? Edit: As a final point, you can do it like this, using regular expressions: <?php $str = 'something1,something2,1\,2\,3'; $bits = split('[^\\],',$str); print_r($bits); ?> Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/#findComment-381270 Share on other sites More sharing options...
Orio Posted October 30, 2007 Share Posted October 30, 2007 The ugly way: <?php $str = "'a1','a2','1,2,3'"; $str = substr($str,0,-1); $str = substr($str,1); $parts = explode("','", $str); echo"<pre>"; print_r($parts); echo "</pre>"; ?> The nice way: <?php $str = "'a1','a2','1,2,3'"; preg_match_all("/'(.*?)(',|'$)/", $str, $matches); echo"<pre>"; print_r($matches[1]); echo "</pre>"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/#findComment-381280 Share on other sites More sharing options...
timbrown Posted October 30, 2007 Author Share Posted October 30, 2007 Thanks, I thought of using regular expressions like this but unfortunately this regular expression also replaces the character before the comma so you end up with "something" instead of "something1". Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/#findComment-381294 Share on other sites More sharing options...
Orio Posted October 30, 2007 Share Posted October 30, 2007 The regular expression way is working perfectly fine... The ugly way is not recommended lol Orio. Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/#findComment-381297 Share on other sites More sharing options...
timbrown Posted October 30, 2007 Author Share Posted October 30, 2007 Hey thanks Orio, I missed your post while I was typing. Works lovely! Thanks very much. Tim. Quote Link to comment https://forums.phpfreaks.com/topic/75375-solved-delimiting-and-explode/#findComment-381300 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.