Php reads excel files and garbled output values

Source: Internet
Author: User
Php reads an excel file and the output value is garbled & lt ;? Php?handlefopen(&quot=.home.csv & quot;, & quot; r & quot;); & nbsp; while ($ itemsfgetcsv ($ handle, 1000) {& nbsp; foreach ($ itemsas $ k & gt; $ v) {& nbsp; php reads the excel file, and the output value is garbled
$ Handle = fopen ("./home.csv", "r ");
While ($ items = fgetcsv ($ handle, 1000 )){
Foreach ($ items as $ k =>v v ){
$ V = iconv ('gb2312', 'utf-8', $ v );
Var_dump ($ v );
}
}

------ Solution --------------------
You're talking about this.
This is a BUG in php5.3, and php5.2 is normal.

You have to change the solution.
------ Solution --------------------
PHP code
$ Fname = $ _ FILES ['myfile'] ['tmp _ name']; // Obtain the uploaded CSV file $ handle = fopen ("$ fname ", "r "); // open the file // Determine whether the imported file type is csv. // if the file type is csv, continue executing if ($ _ FILES ['myfile'] ['type']! = 'Application/vnd. ms-excel ') {if ($ _ FILES ['myfile'] ['type']! = 'Text/comma-separated-values') {echo "script" alert ('your uploaded file type does not match, please upload again '); window. location = 'subhotel _ import. php '; script "; exit () ;}$ num = 1; $ total_num = 100; // set the number of data entries added each time. // delete the original data in the table $ db-> query ("delete from partition _activities_2 "); // open the imported csv file // add it to the table cyclically while (! Feof ($ handle) {$ content = fgets ($ handle); $ data = preg_split ("/,/", $ content ); if ($ data [0] = '') {continue;} $ subpartition _id = preg_replace ('/[^ 0-9]/','', $ data [0]); $ subpartition _type = (int) preg_replace ('/[^ 0-9]/', ', $ data [4]); if ($ num = $ total_num) {$ num = 1; $ values. = "($ subscribe _id ,'". $ data [1]. "','". $ data [2]. "','". $ data [3]. "', $ subinto_type),"; $ values = rtrim ($ values,', '); $ SQL = "insert into hot El_activities_2 (partition _id, start_date, end_date, content, s_type) values ". $ values; $ aaa = $ db-> query ($ SQL); $ values = '';} else {$ values. = "($ subscribe _id ,'". $ data [1]. "','". $ data [2]. "','". $ data [3]. "', $ sub1__type),"; $ num ++ ;}} if (mysql_error! = 1) {$ values = rtrim ($ values, ','); $ SQL = "insert into _activities_2 (partition _id, start_date, end_date, content, s_type) values ". $ values; $ db-> query ($ SQL); echo "script alert ('import successful !!! '); Window. location = 'subhotel _ query_file.php'; script ";} else {echo" script "alert ('import failed !!! '); Window. location = 'subhotel _ import. php'; script ";} fclose ($ handle );
------ Solution --------------------
In the csv format standard: strings must be enclosed in double quotation marks, but Microsoft's tool software does not add this double quotation mark.
In versions earlier than php5.3, php agrees with this practice.
However, since php5.3, Microsoft has not cooperated, so fgetcsv is also disabled.
------ Solution --------------------
A plug-in is required to read excel data.
The following describes how to read ecxcel data to the database:
7. $ handle = fopen (”test.csv "," r ");
8. $ SQL = "insert into scores (idcard, names, num, sex, nation, score) values ('";
9. while ($ data = fgetcsv ($ handle, 1000, ",") {
10. $ num = count ($ data );
11. for ($ c = 0; $ c <$ num; $ c ++ ){
12. if ($ c ==$ num-1) {$ SQL = $ SQL. $ data [$ c]. "')"; break ;}
13. $ SQL = $ SQL. $ data [$ c]. "','";
14 .}
15. print "<br> ";
16. echo $ SQL. "<br> ";
17. $ db-> query ($ SQL );
18. echo "SQL statement execution successful! <Br> ";
19. $ SQL = "insert into scores (idcard, names, num, sex, nation, score) values ('";
20 .}
21. fclose ($ handle );
22. $ time_end = getmicrotime ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.