Jump to content

[SOLVED] parse characters in CSV


lordrt

Recommended Posts

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...

Link to comment
Share on other sites

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();
?>

 

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.