Jump to content

preg troubles


bouwob

Recommended Posts

$fp = fopen('./car.txt','r') or die ("shit wont open");
$fp1 = fopen('./cars1.txt','w');

while (!feof($fp) ) 
{ 
$line = fgets($fp); 
echo preg_replace('/^([0-9]{4})\s([a-zA-Z]+)\s([a-zA-Z]+)\s(\.+)([0-9]{1,2}\.[0-9]{1,2})\s ([0-9]{1,2}\.[0-9]{1,2})$/', '$1,$2,$3,$4,$5,$6', $line) or die("Error querying database");
fwrite($fp1, preg_replace('/^([0-9]{4})\s([a-zA-Z]+)\s([a-zA-Z]+)\s(\.+)([0-9]{1,2}\.[0-9]{1,2})\s([0-9]{1,2}\.[0-9]{1,2})$/', '$1,$2,$3,$4,$5,$6', $line)."\n");
}


fclose($fp);
fclose($fp1);

 

 

What I have

1997 Acura 2.2CL 8.9 16.7
2002 Acura 3.2CL Type S 6.8 15.0
1996 Acura 3.2TL 8.1 16.4
1999 Acura 3.2TL 7.4 15.7
2002 Acura 3.2TL Type S 6.2 14.8
2003 Acura 3.2CL Type-S 6.5 14.9
1986 Acura Integra RS 9.3 17.0
1986 Acura Integra LS 8.8 16.5
1989 Acura Integra LS 9.3 17.1
1990 Acura Integra 3-Dr GS 9.2 16.8

 

 

What I need

1997, Acura, 2.2CL, 8.9, 16.7
2002, Acura, 3.2CL Type S, 6.8, 15.0
1996, Acura, 3.2TL, 8.1, 16.4
1999, Acura, 3.2TL, 7.4, 15.7
2002, Acura, 3.2TL Type S, 6.2, 14.8
2003, Acura, 3.2CL Type-S, 6.5, 14.9
1986, Acura, Integra RS, 9.3, 17.0
1986, Acura, Integra LS, 8.8, 16.5
1989, Acura, Integra LS, 9.3, 17.1
1990, Acura, Integra 3-Dr GS, 9.2, 16.8

 

Question is how can you do it?

 

ereg doesnt seem to work for me so I must have a bug somewhere in there.

 

erag takes the 3rd argument testing against the 1st argument. that is then passed to be replaced by the 2nd

 

are commas key words in regexp? If not why isnt this working? I am very confident in the re's.

Link to comment
Share on other sites

$fp = fopen('./car.txt','r') or die ("shit wont open");
$fp1 = fopen('./cars1.txt','w');

while (!feof($fp) ) 
{ 
$line = fgets($fp); 
#            1988         Acura       Legend       Coupe L            9.6                      17.2
echo preg_replace('^([0-9]{4})\s([a-zA-Z]+)\s(.+)([0-9]{1,2}\.[0-9]{1,2})\s([0-9]{1,2}\.[0-9]{1,2})$', '$1,$2,$3,$4,$5', $line) or die("Error querying database");
fwrite($fp1, preg_replace('^([0-9]{4})\s([a-zA-Z]+)\s(.+)([0-9]{1,2}\.[0-9]{1,2})\s([0-9]{1,2}\.[0-9]{1,2})$', '$1,$2,$3,$4,$5', $line)."\n");
}


fclose($fp);
fclose($fp1);

 

these regexps are good. (awsome tool :)

 

but the file does not write. The "dies" are not showing anything. (ps who do I print a trace and error with a die)

 

What am I doing wrong?

 

tia

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.