0

I'm facing a problem that I can't solve on my own, I can't find a way to do it.

I have those two strings:

'Germany, "Sightseeing, Travelling, Hotels"'

and

'Health, Medicin, Healthcare'

I need to explode() this strings on the , char, but only if the text, where this , lays in isn't surrounded by ".

So, as example this would be the desired results:

array(0 => 'Germany', 1 => '"Sightseeing, Travelling, Hotels"');
array(0 => 'Health', 1 => 'Medicin', 2 => 'Healthcare');

By now this is what I have:

explode(",", 'Germany, "Sightseeing, Travelling, Hotels"');

Which will give out

array(0 => 'Germany', 1 => '"Sightseeing', 2 => 'Travelling', 3 => 'Hotels"');

How can I get to this result?

6
  • 1
    Are you trying to parse a CSV? Commented Sep 1, 2015 at 7:58
  • @deceze I'm going through a CSV line by line, I need to generate categories based on those example string I provided. Now "Sightseeing, Travelling, Hotels" is a single category, not 3 seperated ones. Commented Sep 1, 2015 at 8:01
  • Show the code you have wriiten so far. Commented Sep 1, 2015 at 8:02
  • So, have you tried using a CSV parser? Commented Sep 1, 2015 at 8:02
  • @deceze I didn't know that there are functions for that in PHP and my I couldn't find something in my research. But I never searched for CSV parsing, thanks for pointing it out to me, I'll have a look at it. Commented Sep 1, 2015 at 8:07

2 Answers 2

4

See str_getcsv() how to parse csv (even if you are not reading a csv file, this is exactly what this string looks like):

<?php
print_r(str_getcsv('Germany, "Sightseeing, Travelling, Hotels"', ','));
print_r(str_getcsv('Health, Medicin, Healthcare'));
print_r(str_getcsv('D"uh!, \"Test\"', ','));
?>

results in:

Array ( [0] => Germany [1] => Sightseeing, Travelling, Hotels )
Array ( [0] => Health [1] => Medicin [2] => Healthcare )
Array ( [0] => D"uh! [1] => "Test" ) 

Remarks: This Function is available beginning with php version 5.3.0 and can also be configured as to what your input looks like. I added some special cases, this is a classic problem that looks far simpler than it is.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, this is excactly what I needed, didn't know there's an function for that in PHP.
0

You can use php function str_getcsv.

Here an example:

$string_1 = 'Germany, "Sightseeing, Travelling, Hotels"';
$string_2 = 'Health, Medicin, Healthcare';

$result_1 = str_getcsv($string_1,',','"');
$result_2 = str_getcsv($string_2,',','"');

print_R($result_1);
print_R($result_2);

that outputs:

Array
(
    [0] => Germany
    [1] => Sightseeing, Travelling, Hotels
)
Array
(
    [0] => Health
    [1] =>  Medicin
    [2] =>  Healthcare
)

You can after map each element of the resultant array, and if there is a comma you can surround it with "

foreach($result_1 as $i => $item) {
    if(substr_count($item,',') > 0) {
        $result_1[$i] = '"'.$item.'"';
    }
}

Wich produce:

Array
(
    [0] => Germany
    [1] => "Sightseeing, Travelling, Hotels"
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.