Forum Moderators: coopster

Message Too Old, No Replies

Splitting a string

preg_replace?

         

Paul in South Africa

12:54 pm on Jun 9, 2003 (gmt 0)

10+ Year Member



I have a CSV file that is read line by line using PHP. A very simplified version of the file follows.

1,Test in Progress,2,3,4, , , ,
2, , , , ,Next Test,5,6,

What I need to do is get the line into an array so that I can then process it depending on the first value on the line. The processing involves inserting the data into a database table, the table being dependant on the value.

I think I need to use preg_replace or preg_split to split the line into an array but even after reading the manual and searching other posts here I cannot seem to get the syntax correct. Can anyone point me in the right direction?

eaden

1:02 pm on Jun 9, 2003 (gmt 0)

10+ Year Member



$ar = explode(',',$line);

where $line is one line

Storyteller

1:04 pm on Jun 9, 2003 (gmt 0)

10+ Year Member



Just don't forget to handle escaped commas ;)

Paul in South Africa

1:20 pm on Jun 9, 2003 (gmt 0)

10+ Year Member



Thanks for that guys, it worked like a charm, but now the programmer of the application that writes the file to the webserver has changed things again. A line of the file is now going to look like this

2,,,,,"Next Test",5,6,

where any text is quoted and could possibly contain commas and numerals will not be quoted. How do I now get the text parts out without it splitting at any commas that might be there?

Thanks

eaden

1:36 pm on Jun 9, 2003 (gmt 0)

10+ Year Member



Hmm I guess that is where you would use preg_split. However my regex knowlege is not very good so I'll offer two other ideas,

Phpmyadmin has this functionality, either copy their code, or using phpmyadmin, insert the CSV into a tempory table, and then using php, select all the records and do what you want with them.

ShawnR

2:59 pm on Jun 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_match_all("/[^,]\"(.*?)\"[,$]¦[^,](\d*)[,$]/",
$your_input_string,
$out);

(replace the ¦ with the proper pipe)
Untested, but I don't think I've made any errors...

Then

$out[1][0] will be the first field,
$out[1][1] will be the second field,
etc

Shawn

<added: Just to explain what it is doing so you can do future maintenance:

[^,]\"(.*?)\"[,$]¦[^,](\d*)[,$]
means either [^,]\"(.*?)\"[,$] or [^,](\d*)[,$]

[^,]\"(.*?)\"[,$] means:

  • [^,] - i.e. either the start of the string (^) or a comma
    followed by
  • \" - i.e. the quotes (") ( \ escapes it)
    followed by
  • (.*?) - i.e. any character (.) repeated 0 or more times (*) but as few times as possible (?). The brackets "(" and ")" tell it 'this is the stuff I'm interested in.'
    followed by
  • \" - i.e. another set of quotes
    followed by
  • [,$] - i.e. either the end of the string ($) or a comma

In the second part, \d matches a digit, and * again means 0 or more times.

Paul in South Africa

3:33 pm on Jun 9, 2003 (gmt 0)

10+ Year Member



Thanks for the explanation Shawn, it has already been printed for reference.

It almost works but not quite. I have not tried to get it to insert anything into the database yet, I am just trying to echo the elements of the array to make sure that they are there.

I tried
if ($out[1][0] == 1)
{
code to echo array elements
}
elseif ($linea[1][0] == 2)
{
code to echo array elements
}

and nothing happened.

I then tried

if ($out[0][0] == 1)
{
code to echo array elements
}
elseif ($linea[0][0] == 2)
{
code to echo array elements
}

which at least resolves the if statement to true when it is true but I must be doing something wrong with trying to get the array elements to display in the browser.

print "$out[0][1] Parameters $out[0][2], $out[0][3], $out[0][4]<br>";

results in

Array[1] Parameters Array[2], Array[3], Array[4]

being displayed in the browser. Any ideas?

I'm feeling very stupid this evening after a very long day so any help will be much appreciated.

Paul in South Africa

3:38 pm on Jun 9, 2003 (gmt 0)

10+ Year Member



Fixed it I think. It didn't like the double quotes in the print. I told you I was feeling stupid today ;)

<added>Not quite fixed. The numbers are fine but the text parts seem to have gone missing. I thing I'll go and have a cold beer and start again tomorrow.</added>