bouwob Posted February 21, 2007 Share Posted February 21, 2007 $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. Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 Not sure if this regex will work for you or not: ^([0-9]{4}) ([^\s]+) ([a-z0-9\.]+[\s]?[^[\d]) ([a-z0-9\.]+) ([a-z0-9\.]+) Your replace would be $1, $2, $3, $4, $5 Tip: http://weitz.de/regex-coach/ Thats an extremely powerful tool for figuring out regular expressions. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 $data = preg_replace('/^(\d+)\s+([A-Za-z]+)\s+(.+)\s+([\d.]+)\s+([\d.]+)\z/', '\1, \2, \3, \4, \5', $data); Quote Link to comment Share on other sites More sharing options...
bouwob Posted February 21, 2007 Author Share Posted February 21, 2007 $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 Quote Link to comment Share on other sites More sharing options...
effigy Posted February 21, 2007 Share Posted February 21, 2007 You don't have dies on the second fopen or the fwrite. 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.