/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] ereg issues

My current code

[code=php]

$fp = fopen(‘./car.txt’,’r’);
$fp1 = fopen(‘./cars1.txt’,’w’);

while (!feof($fp) )
{
$line = fgets($fp);
# 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})$’;
echo ereg_replace ($re1, $re2, $line);
fwrite($fp1, ereg_replace ($re1, $re2, $line).”n”);
}

fclose($fp);
fclose($fp1);
[/code]

What I have

[code]
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
[/code]

What I need

[code]
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
[/code]

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.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 21.2007 — What exactly is it that you want to do? (What do the original lines look like, and what do you want them to end up as?)

One thing I noticed is that it appears you have some extraneous space characters in the regex, each of which will be treated as a literal space character.

Also, I'm not sure if the ereg functions support the escape sequences such as "s". You may need to use the preg functions instead.
Copy linkTweet thisAlerts:
@DARTHTAMPONauthorFeb 21.2007 — [code=php]
$fp = fopen('./car.txt','r') or die ("it wont open");
$fp1 = fopen('./cars1.txt','w') or die ("it wont open either");

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("preg wont happen");
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);
[/code]


now code. Now nothing happens. I replaced $re2 to use regexp veriables which I guess is what I need to do. Also on a side note is there a system veriable that will give me a stack trace and error?
×

Success!

Help @DARTHTAMPON spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...