lordrt Posted August 10, 2009 Share Posted August 10, 2009 I have a csv file which contains data in the following format: '<p>Hamburger nature 45</p>', 'img src= "some path"', .... These data will be stored in a mysql db, and am using the following test code to read and display csv data in my browser: <?php // import php file: import_article.php require("import_article.php"); // read first .csv file encountered $path = "sites/path/files/"; $sfile = "*.csv"; foreach (glob($path.$sfile) as $filename){ echo $filename . "<br/>"; $file = fopen($filename, 'r'); while (($data = fgetcsv($file, 0, ",", "'")) !== False) { //$data = str_replace("'", " ", $data); echo $data[0]. $data[1]. $data[2]. $data[3]. $data[4]. $data[5]. $data[6]. "<br/>"; } echo "read complete"; fclose($file); // unlink ($filename); } // echo "closing connection"; // mysql_close(); ?> I however have been asked to strip the whitespaces present, I investigated trim() for this, and now have to read first and last chars, if they are single quotes, replace them with blank values, and if using french words, like l'église, the ' must not be removed as it forms part of the data. Can anyone help me with the part to read first and last chars and if ' remove them?? :-\ Also need to cater for the commas present and remove them as well... Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 10, 2009 Share Posted August 10, 2009 Why have you got the length set as 0??? <?php // import php file: import_article.php require("import_article.php"); // read first .csv file encountered $path = "sites/path/files/"; $sfile = "*.csv"; foreach (glob($path.$sfile) as $filename){ echo $filename . "<br/>"; $file = fopen($filename, 'r'); while (($data = fgetcsv($file, 1024, ",", "'")) !== False) { //$data = str_replace("'", " ", $data); ltrim($data, "'"); rtrim($data, "'"); for ( $n = 0; $n < count($data); $n++ ) { echo $data[$n]; } echo '<br />'; } echo "read complete"; fclose($file); // unlink ($filename); } // echo "closing connection"; // mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 10, 2009 Share Posted August 10, 2009 Sorry, forgot the data was an array, that should work. <?php // import php file: import_article.php require("import_article.php"); // read first .csv file encountered $path = "sites/path/files/"; $sfile = "*.csv"; foreach (glob($path.$sfile) as $filename){ echo $filename . "<br/>"; $file = fopen($filename, 'r'); while (($data = fgetcsv($file, 1024, ",", "'")) !== False) { //$data = str_replace("'", " ", $data); foreach($data as $k => $v) { $data[$k] = ltrim($v, "'"); $data[$k] = rtrim($v, "'"); } $data = implode(" ", $data); echo $data . '<br />'; } echo "read complete"; fclose($file); // unlink ($filename); } // echo "closing connection"; // mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
lordrt Posted August 10, 2009 Author Share Posted August 10, 2009 Sorry, forgot the data was an array, that should work. <?php // import php file: import_article.php require("import_article.php"); // read first .csv file encountered $path = "sites/path/files/"; $sfile = "*.csv"; foreach (glob($path.$sfile) as $filename){ echo $filename . "<br/>"; $file = fopen($filename, 'r'); while (($data = fgetcsv($file, 1024, ",", "'")) !== False) { //$data = str_replace("'", " ", $data); foreach($data as $k => $v) { $data[$k] = ltrim($v, "'"); $data[$k] = rtrim($v, "'"); } $data = implode(" ", $data); echo $data . '<br />'; } echo "read complete"; fclose($file); // unlink ($filename); } // echo "closing connection"; // mysql_close(); ?> Thx Andy-H, will try it Quote Link to comment Share on other sites More sharing options...
lordrt Posted August 10, 2009 Author Share Posted August 10, 2009 A small qu yet again Since am using $data[0]. $data[1]. $data[2]. $data[3]. $data[4]. $data[5]. $data[6], will the foreach part have to be changed?? Quote Link to comment Share on other sites More sharing options...
lordrt Posted August 10, 2009 Author Share Posted August 10, 2009 A small qu yet again Since am using $data[0]. $data[1]. $data[2]. $data[3]. $data[4]. $data[5]. $data[6], will the foreach part have to be changed?? found the mistake, should have commented the implode part Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.