Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting bettwen comma problem.

Status
Not open for further replies.

altendew

Programmer
Mar 29, 2005
154
US
Hi, I currently want to split between commas and return it an array, this can accomplished with this.

Code:
$string = "'i like birds, and dogs','col2 value','col3 value'";
$array = explode(',',$string);

Now I want to return like this:
Code:
Array (0 => 'i like birds, and dogs',
       1 => 'col2 value',
       2 => 'col3 value')

But it's returning as this.
Code:
Array (0 => 'i like birds',
       1 => 'and dogs',
       2 => 'col2 value',
       3 => 'col3 value')
 
My bad.. its returning like this:
Code:
Array (0 => 'i like birds,
       1 =>  and dogs',
       2 => 'col2 value',
       3 => 'col3 value')
 
bit of a kludge
Code:
$string = "'i like birds, and dogs','col2 value','col3 value'";
$separator = "~~##";
$array = explode($separator, str_replace ("',", $separator, $string));

there are good csv parsing functions for file resources in php. take a look at fget_csv
 
Hmm, a bit late but:

if you definately have quotes around every value and none in the value, you could:
Code:
$array = explode("','",$string)

or to remove the beginning and trailing quote also:
$array = explode("','",substr($string,1,-1))

or to keep your quotes and commas:
preg_match_all("/'[^']+',?/",$string,$array);

Just playing around, I only get to play with PHP occasionally and needed a break from the ugliness that is ASP.Net :)

signature.png
 
See the values all dont have quotes around them.

It can be like..

'1',"2",3

Thanks for all your work so far, very useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top