mikebyrne Posted April 4, 2009 Share Posted April 4, 2009 Im tring to create a comma seperate file to insert into a database but at present im manually putting the commas in. My test file looks like: 2981 Rxxxe, Vixxxnt 1 Flixxxxs Clxxxe Axxxy Co.Kildare 2982 Bxxxxe, Anxxxxe 2 Flxxxrs Cxxxxe Axxxy Co.Kildare L 2983 Vxxxie, Alxxxxra 4 Flixxxxs Cxxxxe Axxxxy Co.Kildare But I want the spacing to look like this: ,2981, Rxxxe, Vixxxnt, 1 Flixxxxs Clxxxe, Axxxy, Co.Kildare ,2982, Bxxxxe, Anxxxxe, 2 Flxxxrs Cxxxxe, Axxxy, Co.Kildare L ,2983, Vxxxie, Alxxxxra, 4 Flixxxxs Cxxxxe, Axxxxy, Co.Kildare Is there a way of coding this in PHP? Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 4, 2009 Share Posted April 4, 2009 as stated above sorry for trying. example only. <?php $word="php is the best"; $test=explode(' ',$word); $res=implode(',',$test); print_R($res); ?> a example to re order the result. <?php $word="php is the best"; $test=explode(' ',$word); $res=implode(',',$test); $t=explode(',',$res); print_R($t); ?> array result Array ( [0] => php [1] => is [2] => the [3] => best ) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 4, 2009 Share Posted April 4, 2009 That will replace all spaces with commas. That is not what the OP wants. Ken Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 4, 2009 Share Posted April 4, 2009 How is the test file created? To be able to do what you want to do, you need to be able to delimit the different groups of words better. Is there a pattern that can be keyed on? Ken Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 there's no real delimiter set just the spaces between the words hence me manually placing in the commas. Any ideas? I used a program to rip the data from a pdf and it is now displayed 2981 Rxxxe, Vixxxnt 1 Flixxxxs Clxxxe Axxxy Co.Kildare 2982 Bxxxxe, Anxxxxe 2 Flxxxrs Cxxxxe Axxxy Co.Kildare L 2983 Vxxxie, Alxxxxra 4 Flixxxxs Cxxxxe Axxxxy Co.Kildare my db has the 8 fields Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 Any help on this would be great Quote Link to comment Share on other sites More sharing options...
laffin Posted April 4, 2009 Share Posted April 4, 2009 Looking at it I do see the delimeter, double spaces problem will be the comma between first & last name, this means ya will have to replace the comma with something else or in yer csv document make room for both first & last name u can use implode,explode or str_replace for the double spaces good luck Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 So if i was to remove the comma how can i code the php to put the comma in the double spaces? Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 4, 2009 Share Posted April 4, 2009 were all working it out, it takes time wait Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 thanks for the help guys! Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 4, 2009 Share Posted April 4, 2009 got this so far have a look. <?php $field[]=" 2981 Rxxxe, Vixxxnt 1 Flixxxxs Clxxxe Axxxy Co.Kildare"; $field[]="2982 Bxxxxe, Anxxxxe 2 Flxxxrs Cxxxxe Axxxy Co.Kildare"; $field[]="L 2983 Vxxxie, Alxxxxra 4 Flixxxxs Cxxxxe Axxxxy Co.Kildare"; $test1=preg_replace("/L/"," ",$field); $res2=implode(' ',$test1); $res3=preg_replace("/\s/",",",$test1); $test4=preg_replace("/,,/",",",$res3); $test5=preg_replace("/,,/",",",$test4); $test6=preg_replace("/^[,]{1}/"," ",$test5); echo"<PRE>"; print_r($test6); echo"</PRE>"; ?> result. <PRE>Array ( [0] => 2981,Rxxxe,Vixxxnt,1,Flixxxxs,Clxxxe,Axxxy,Co.Kildare [1] => 2982,Bxxxxe,Anxxxxe,2,Flxxxrs,Cxxxxe,Axxxy,Co.Kildare [2] => 2983,Vxxxie,Alxxxxra,4,Flixxxxs,Cxxxxe,Axxxxy,Co.Kildare ) </PRE> Quote Link to comment Share on other sites More sharing options...
charleshill Posted April 4, 2009 Share Posted April 4, 2009 Where is the data coming from that goes into this file? Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 4, 2009 Share Posted April 4, 2009 he getting info from a phased pdf file. Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 4, 2009 Share Posted April 4, 2009 all i can do sorry. <?php $field[]=" 2981 Rxxxe, Vixxxnt 1 Flixxxxs Clxxxe Axxxy Co.Kildare"; $field[]="2982 Bxxxxe, Anxxxxe 2 Flxxxrs Cxxxxe Axxxy Co.Kildare"; $field[]="L 2983 Vxxxie, Alxxxxra 4 Flixxxxs Cxxxxe Axxxxy Co.Kildare"; $test1=preg_replace("/L/"," ",$field); $res2=implode(' ',$test1); $res3=preg_replace("/\s/",",",$test1); $test4=preg_replace("/,,/",",",$res3); $test5=preg_replace("/,,/",",",$test4); $test6=preg_replace("/^[,]{1}/"," ",$test5); $tested[]=preg_replace("/[,]{1}(1)[,]{1}/"," 1 ",$test6); $tested[]=preg_replace("/[,]{1}(2)[,]{1}/"," 2 ",$test6); $tested[]=preg_replace("/[,]{1}(4)[,]{1}/"," 4 ",$test6); $tested=$tested[0][0].'<br>'.$tested[1][1].'<br>'.$tested[2][2]; echo"<PRE>"; print_r($tested); echo"</PRE>"; ?> result <PRE> 2981,Rxxxe,Vixxxnt 1 Flixxxxs,Clxxxe,Axxxy,Co.Kildare<br>2982,Bxxxxe,Anxxxxe 2 Flxxxrs,Cxxxxe,Axxxy,Co.Kildare<br> 2983,Vxxxie,Alxxxxra 4 Flixxxxs,Cxxxxe,Axxxxy,Co.Kildare</PRE> Quote Link to comment Share on other sites More sharing options...
laffin Posted April 4, 2009 Share Posted April 4, 2009 As I stated above, I noticed that the delimeter was double spaces. u can do it with str_replace instead of using all those preg_replace statements <?php $fields[]=' 2981 Rxxxe, Vixxxnt 1 Flixxxxs Clxxxe Axxxy Co.Kildare'; $fields[]=' 2982 Bxxxxe, Anxxxxe 2 Flxxxrs Cxxxxe Axxxy Co.Kildare'; $fields[]='L 2983 Vxxxie, Alxxxxra 4 Flixxxxs Cxxxxe Axxxxy Co.Kildare'; foreach($fields as $i=>$field) { $field=str_replace(' ',',',$field); $field=str_replace(', ',',',$field); $fields[$i]=$field; } print_r($fields); ?> which generates: Array ( [ 0] => ,2981,Rxxxe,Vixxxnt,1,Flixxxxs Clxxxe Axxxy Co.Kildare [ 1] => ,2982,Bxxxxe,Anxxxxe,2,Flxxxrs Cxxxxe Axxxy Co.Kildare [ 2] => L,2983,Vxxxie,Alxxxxra,4,Flixxxxs Cxxxxe Axxxxy Co.Kildare ) Note that we compress the comma space sequence for first and last name Quote Link to comment Share on other sites More sharing options...
redarrow Posted April 4, 2009 Share Posted April 4, 2009 you can not there no , comma at the beginning and there no L and also all the xxxx letters need , comma like you said what sorry? wanted result. ,2981, Rxxxe, Vixxxnt, 1 Flixxxxs Clxxxe, Axxxy, Co.Kildare ,2982, Bxxxxe, Anxxxxe, 2 Flxxxrs Cxxxxe, Axxxy, Co.Kildare L ,2983, Vxxxie, Alxxxxra, 4 Flixxxxs Cxxxxe, Axxxxy, Co.Kildare please tell php freaks how your's matches. MEMBER LAFFIN Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 Would there be a way of inputing the file instead of typing the below? (sometihng like $fields[] = 'C:\desktop\filename.txt') $fields[]=' 2981 Rxxxe, Vixxxnt 1 Flixxxxs Clxxxe Axxxy Co.Kildare'; $fields[]=' 2982 Bxxxxe, Anxxxxe 2 Flxxxrs Cxxxxe Axxxy Co.Kildare'; $fields[]='L 2983 Vxxxie, Alxxxxra 4 Flixxxxs Cxxxxe Axxxxy Co.Kildare'; Quote Link to comment Share on other sites More sharing options...
laffin Posted April 4, 2009 Share Posted April 4, 2009 That depends on the size of the file yer trying to read the code posted just shows different ways of parsing the lines if its small, able to fit in memory u can use many of the code excerpts, and replace the array with $fields=file('filename.txt'); however if the file is larger, u will need to parse it line by line as well as outputting it. with fopen, fgets, fputs and fclose Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 Yeah the file only contains about 3,000 lines if that. How do I send the output to a file instead of displaying it in a browser? Quote Link to comment Share on other sites More sharing options...
laffin Posted April 4, 2009 Share Posted April 4, 2009 I dunno any function that takes an array to file output (the inverse of file). u can do it line by line or implode the array, and use file_put_contents. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 4, 2009 Share Posted April 4, 2009 The way you write out data in an array to a file is: <?php file_put_contents('/path/to/your/file',implode("\r\n",$array_containing_lines)); ?> The above assumes there are no line terminating characters at the end of the lines, if there are omit the "\r\n". Ken Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 5, 2009 Author Share Posted April 5, 2009 I now have the following code but getting the error Warning: implode() [function.implode]: Argument must be an array in C:\xampp\htdocs\commainsert.php on line 9 My code is: <?php $fields=file('C:\Users\Mike\Desktop\5april.txt'); foreach($fields as $i=>$field) { $field=str_replace(' ',',',$field); $field=str_replace(', ',',',$field); $fields[$i]=$field; } file_put_contents('C:\Users\Mike\Desktop\test.txt',implode("\r\n",$array_containing_lines)); ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 5, 2009 Share Posted April 5, 2009 You weren't supposed to take the "$array_containing_lines" literally, you have to replace it with the name of your array, in your case "$fields". Ken Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 5, 2009 Author Share Posted April 5, 2009 I've got the code working but the out put looks like: ,2947,Bxxxy,Bxxry,20 Dun Brxxxnn,Sunxxxide Lawns Axxy Co.Kilxxxxre ,2948,Bxxxy,Isxxxl,20 Dun Brxxxn,Sunxxxide Lawns Axxy Co.Kilxxxxe It doesnt seem to put the commas near the end ie ,2948,Bxxxy,Isxxxl,20 Dun Brxxxn,Sunxxxide Lawns, Axxy, Co.Kilxxxxe Is it because Axxy Co.Kilxxxxe are single spaced?? Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 5, 2009 Author Share Posted April 5, 2009 Problem solved! Thanks for all the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.