Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP - > Upload process and create a new csv file

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hello, i have an csv document with 2 columns that have the following example data:-

REF OEM
987392405 DSB52402402404240 / LR2401240574 / SOB52402402404240 / SOB524024024042
987246240 1240240196240 / 1H240615423A / 1H240615423B / 1H240615423D / 1H240615423DX / 6K240615423B / 1G240615423 / 7M240615423
etc...

In a php page, i want to select a csv file, process it and then create a new csv file which has been processed to look like the following?

REF OEM
987392405 DSB52402402404240
987392405 LR2401240574
987392405 SOB52402402404240
987392405 SOB524024024042

Please can anyone show me how to do it?

many thanks

BF
 
Hi

Personally I would do it like this :
Code:
[blue]master #[/blue] php -R '[b]if[/b][teal]([/teal][navy]$argi[/navy][teal]==[/teal][purple]1[/purple][teal])[/teal][b]echo[/b][i][green]"$argn\n"[/green][/i][teal];[/teal][b]else[/b][teal]{[/teal][navy]$p[/navy][teal]=[/teal][COLOR=orange]explode[/color][teal]([/teal][i][green]"\t"[/green][/i][teal],[/teal][navy]$argn[/navy][teal],[/teal][purple]2[/purple][teal]);[/teal][b]foreach[/b][teal]([/teal][COLOR=orange]explode[/color][teal]([/teal][i][green]" / "[/green][/i][teal],[/teal][navy]$p[/navy][teal][[/teal][purple]1[/purple][teal]])[/teal][b]as[/b][navy]$o[/navy][teal])[/teal][b]echo[/b][i][green]"$p[0]\t$o\n"[/green][/i][teal];}[/teal]' < Brianfree.csv
REF             OEM
987392405       DSB52402402404240
987392405       LR2401240574
987392405       SOB52402402404240
987392405       SOB524024024042
987246240       1240240196240
987246240       1H240615423A
987246240       1H240615423B
987246240       1H240615423D
987246240       1H240615423DX
987246240       6K240615423B
987246240       1G240615423
987246240       7M240615423
Assuming that the column separator is always a tab and the OEM's internal separator is always space+slash+space. Some error checking may help though.

If need help adapting it to your context, let us know. ( And please post preformatted text between [tt][ignore][pre][/ignore][/tt] and [tt][ignore][/pre][/ignore][/tt] TGL tags. )


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top