Forum Moderators: coopster

Message Too Old, No Replies

Importing a pipe-separated file to database issue

         

ollyno1uk

11:56 am on Apr 3, 2009 (gmt 0)

10+ Year Member



Hi

I wrote a script a while back that imports a pipe csv files into my database. It worked great until the people who make the file started using some pipes within the string.

The strings are separated with quotations but I do not know how to modify this script to not explode the pipes that are contained within;

Perhaps someone can point me in the right direction.


$url = 'url of feed';
$content = file_get_contents($url);

$array = explode("\n", $content);

foreach($array as $line_number=>$line){


$column = explode('¦', $line);



$query = "insert into table set
column1 = $column[0],
column2 = $column[1],
column3 = $column[2];

rocknbil

3:35 pm on Apr 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a knee-jerk, duct-tape approach, but did you try:

Store these in variables so if you ever change it it can easily be updated. Alternatively (recommended,) allow the user to select their separator, and select the qualifier character at the front end so this doesn't come back to haunt you again. Then you just set them from get/post/request.

$separator = '"¦"'; // that's single-quote, double quote,. pipe, double quote, single quote.
$qualifier = '"';

$column = explode($separator, $line);

$column[0] = preg_replace("/^$qualifier(.*)/",$1,$column[0]);

$column[count($column)-1] = preg_replace("/(.*)$qualifier$/",$1,$column[count($column)-1]);

Basically, explode on the separator and strip the separator off the first and last item in the row. The above regexps mean

^ - starts with
$qualifier - followed by $qualifier
(.*) - followed by zero or more of any character, () stores it in $1

Reverse for last item in row, with $ at the end meaning "ends with." count() gives you the count of the array, and arrays are zero based, so one way to get the index of the last item in any array: count-1.

Probably more graceful ways to do it.

[edited by: rocknbil at 3:44 pm (utc) on April 3, 2009]

ollyno1uk

3:43 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



Thanks, after trying a lot of different things I resorted to rewriting it using fgetcsv.

Your way does make sense though and I thank you for taking the time to responsd.

rocknbil

3:45 pm on Apr 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Definitely more graceful. :-) You might consider the comments about how you set your delimiter and enclosure though.