Can you split a string by commas but ignore any commas within a value

Hi guys,

Is there a way you can split a string by comma’s but some how ignore any commas within the value IE

bloggs,bloggs@email,com,555,1,000

I would want an array with 4 elements

[bloggs]
[bloggs@email,com]
[555]
[1,000]

Thanks in advance

Terry

OK. In that case you’ll need to take care of WHICH columns are missing. If it’s always the last 1, 2 or 3 columns missing (in that order) then it’s OK - the array length will just be reduced. If any of the inner columns are missing then the array length may be reduced and thus the array indices may be mixed up. (If the data is missing in the CSV but the comma separators are still there, then there’s nothing really to worry about).

bloggs,bloggs@email.com,555,1,000 — full data, array indices 0 to 3
bloggs,bloggs@email.com,555, — reduced data, array indices 0 to 2
bloggs,bloggs@email.com,1,000 — missing column, array indices 0 to 2, index 2 has wrong data (1,000 instead of 555)
bloggs,bloggs@email.com,1,000 — full data, array indices 0 to 3, index 2 has empty data
bloggs,bloggs@email.com, — full data, array indices 0 to 3, indices 2 and 3 have empty data

Thanks again for your replies guys

siteguru, I just meant there could be a variable amount of columns

http://php.net/manual/en/function.fgetcsv.php

When you say the data is variable, in what way? Can you show some examples of how the data lines might be different?

Why is it email,com and not email.com? I know , is often used in place of . in some regions, but email,com is not a valid format as far as I am aware. This suggests to me you should validate user input to reformat email,com to email.com - this would resolve your initial problem too. :slight_smile:

Is this data stored in a database or in a flat text file? If a database then it suggests that your database table structure is poorly configured if it is returning data like bloggs,bloggs@email,com,555,1,000, as such issues should not occur with a properly-designed database. :frowning:

Sorry guys this was mistype it was meant to be email.com :blush:

The data would be coming in from CSV, so the amount of data could be variable?

Thanks for you replies guys

If a value in a CSV format contains the character used as a delimiter, then the value should be enclosed (usually in quotes). Otherwise it’s improperly formatted and there’s no way to distinguish between delimiter and a “regular character”.

Think of it this way: would you be able to know if this:


1,2,3,4,5

is CSV version of (one value per line):


1
2
3
4
5

or


1,2
3
4,5

? I didn’t think so. A computer program won’t be able to know either, because there is no regular pattern.

The solution is properly formatted CSV:


"1,2",3,"4,5"

or


"1,2","3","4,5"

Alternatively, you can escape special characters that are to be treated literally, for example:


1\\,2,3,4\\,5

or use a different delimiter, which doesn’t occur in values:


1,2|3|4,5

For funz.


<?php
error_reporting(-1);
ini_set('display_errors', true);

$elements = explode(
  '|',
  vsprintf(
    '&#37;s|%s,%s|%s|%s,%s',
    explode(
      ',',
      'bloggs,bloggs@email,com,555,1,000'
    )
  )
);

print_r(
  $elements
);

/*
  Array
  (
      [0] => bloggs
      [1] => bloggs@email,com
      [2] => 555
      [3] => 1,000
  )
*/
?>

However, to the original question here is a quick and dirty method. (The print_array() function is just to demonstrate what is happening).

$str = 'bloggs,bloggs@email,com,555,1,000';
$arr = explode (',', $str);
print_array ($arr);

$str = str_replace (',co', '.co', $str);
$arr = explode (',', $str);
$arr[1] = str_replace ('.co', ',co', $arr[1]);
print_array ($arr);

function print_array ($in) {
	echo '<pre>';
	print_r ($in);
	echo '</pre>';
}