PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm using PHP to read row by row a text file and insert it in my database
everything is working fine, BUT ! i've noticed that about 10 columns have a return charachter - that's making a problem because the php consider these return charachters as a new line (new row)
example:
apple 1 fruit
orange 2 fruit
bana
na 1 fruit
___________________
Read file line by line. And check the number of the columns in the line. If it's good insert it into DB, otherwise collect in temporary array until size of this array becomes correct.
It says, literally, to match anything up to a numeric character, then that numeric character, then anything up to a newline character, and it captures the entire pattern except for the newline character. The '$1|' means to take the first captured pattern (hence the '1') and a pipe character ('|'), and replace the entire matched expression with the new one (as preg_replace is made to do).
Then work with it until it does. The problem is in the regex... I think you need to add the 's' modifier to it. And, if the file doesn't end with a newline, it will miss the last entry.
Gente wrote:One more thing. What is the correct data in you example:
apple 1 fruit
orange 2 fruit
banana 1 fruit
or
apple 1 fruit
orange 2 fruitbana
na 1 fruit
?
OH NO
this is it:
row[0] apple 1 fruit
row[1] orange 2 fruit
row[2] bana
row[3] na 1 fruit
Seems you didn't understand me.
If the $row you posted is correct there's nothing to discuss. I just want to pay your attention that it's easy to see that in your example wrong break is in the line 2. In the real situation as I understood it could be in the line 1. So how do you propose to solve this situation?