Click to See Complete Forum and Search --> : [RESOLVED] regexp help


DARTHTAMPON
02-19-2007, 07:55 PM
$fp = fopen('car.txt','r');
$fp1 = fopen('cars1.txt','w');
while (!feof($fp))
{
$line = fgets($fp); //use 2048 if very long lines
# 1988 Acura Legend Coupe L 9 . 6 17 . 2
$re1 = '^([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})$';
$re2 = '^([0-9]{4} , [a-zA-Z]+ , [a-zA-Z]+ ,) \.+ ([0-9]{1,2}\.[0-9]{1,2}, [0-9]{1,2}\.[0-9]{1,2})$';
# 1988,Acura, Legend, Coupe L, 9.6, 17.2
fwrite($fp1, ereg_replace ($re1, $re2, $line)."\n");
}
fclose($fp);
fclose($fp1);




?>

the string comes in as $re and needs to be changed to $re1 (basicly the noted \s need to become ','. When I run this I get an exact printout of the original record. Why is it not doing anything? The regexp seems to be allright when validated.


any ideas?

NogDog
02-19-2007, 08:18 PM
From the error messages, it looks like you're failing to open the cars.txt file. You might want to add some error-checking there. (Maybe do a is_readable() on it first and check the result.)

DARTHTAMPON
02-19-2007, 08:58 PM
permissions are 644

do I need to have write permissions to read the file?

also

$line = fgets($fp, 1024);

this line (I think) takes the first 1024 characters. Is there a way for me to just read an entire line. I am thinking this could be an issue since each record is probably less than 200

NogDog
02-19-2007, 09:29 PM
644 should give read permission to all (plus write permission to the file owner). Maybe there's just a spelling error in the file name or something? (They're case-sensitive on *nix systems, so be careful with that.)

If you're using a PHP version later than 4.3.0, you can just leave out the 2nd arg to fgets() and it will read to the next newline or EOF.

DARTHTAMPON
02-20-2007, 10:25 AM
$fp = fopen('car.txt','r');
$fp1 = fopen('cars1.txt','w');
while (!feof($fp))
{
$line = fgets($fp); //use 2048 if very long lines
#record 1 1988 Acura Legend Coupe L 9 . 6 17 . 2
$re1 = '^([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})$';
$re2 = '^([0-9]{4} , [a-zA-Z]+ , [a-zA-Z]+ ,) \.+ ([0-9]{1,2}\.[0-9]{1,2}, [0-9]{1,2}\.[0-9]{1,2})$';
# should turn into 1988,Acura, Legend, Coupe L, 9.6, 17.2
fwrite($fp1, ereg_replace ($re1, $re2, $line)."\n");
}
fclose($fp);
fclose($fp1);


the string comes in as $re and needs to bechanged to $re1 (basicly the noted \s need to become ','. When I run this I get an exact printout of the original record. Why is it not doing anything? The regexp seems to be allright when validated.