2

I know how to handle string with comma into csv, and string with double quote into csv,

but if a string with both comma and double quote, how to convert it ?

like:

Seminar on "Changing Myanmar: Institutions, Personalities and Expectations" 10 Feb 2012;

by default I added " " to the string, so it can convert to csv file with comma,

but if this string also contains "", then the field separate into two fields in csv.

I tried this:

"Seminar on """Changing Myanmar: Institutions, Personalities and Expectations""" 10 Feb 2012"

but it is wrong...

1
  • 1
    Your data is ambiguous. You won't be able to reliably decode from it without some knowledge about the fields. If you can re-export your file, use an escape character, such as a backslash. Commented Feb 4, 2013 at 5:25

2 Answers 2

1

I think that using fputcsv() is the best way to avoid any problem.

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

1 Comment

This un-implemented hint could have been a comment under the question instead of an answer.
0

This works:

<?php
@unlink( 'test.csv' );
$fp = fopen( 'test.csv', 'w' );

$text = '"Changing Myanmar: Institutions, Personalities and Expectations"';

fwrite( $fp, '"'. str_replace( '"', '""', $text ) . '", "test"' . "\n" );

fclose( $fp );
?>

Reading from this csv file:

$fp = fopen( 'test.csv', 'r' );

while( ( $data = fgetcsv( $fp, '', ',' ) ) !== false ) {
    echo $data[0];
}

fclose( $fp );

Output - "Changing Myanmar: Institutions, Personalities and Expectations"

Hope this helps.

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.