Jump to content

[SOLVED] delimiting and explode


timbrown

Recommended Posts

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.

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);
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.