Jump to content

[SOLVED] Creating a comma seperated file?


mikebyrne

Recommended Posts

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?

Link to comment
Share on other sites

 

 

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
)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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> 

 

 

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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));
?>

 

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.