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
https://forums.phpfreaks.com/topic/39426-preg-troubles/
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
https://forums.phpfreaks.com/topic/39426-preg-troubles/#findComment-190237
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.