Jump to content

[SOLVED] Strange, values repeating??


mikebyrne

Recommended Posts

I have a file that looks something like the following:

 

D,2796,Son,Oler,13 Dun Bnn,Bch Road,Ahy,Co.Kire

S,2797,Gerty,Laurce,15  Dun Brn,Bleach ad,Ahy,Co.Kilde

L,2801,Mazse,Saras,17  Dn Brn,Blch Rod,Aty,Co.Kilre

E,2808,Esjo,Leel,21  Dun Bnn,Blach Road,Ay,Co.Kilre

 

What I am trying to do is replace the single character letters "D","S","L" & "E" with the following sentences

 

Dail European Parliament and Local Elections only  (instead of D)

 

European Parliament and Local Elections only (instead of E)

 

Local Elections only (instead of L)

 

Post or special arrangement only (instead of S)

 

with lots of help we compiled the following

 

<?php
$rewrote = "";
$handle = fopen("C:\Users\Mike\Desktop\AthyDB.txt", "r+"); // Open file to read it read.

if ($handle) {
while (!feof($handle)) // Loop til end of file.
{
$currentline = fgets($handle, 4096); // Read a line.
$currentline=preg_replace('/^D/','Dail European Parliament and Local Elections only', $currentline);
$currentline=preg_replace('/^S/','Post or special arrangement only', $currentline);
$currentline=preg_replace('/^L/','Local Elections only', $currentline);
$currentline=preg_replace('/^E/','European Parliament and Local Elections only', $currentline);
$rewrote .= $currentline;
$rewrote .= "\n";
}
file_put_contents('C:\Users\Mike\Desktop\test.txt', $rewrote);
fclose($handle);
}
?>

 

It seems that the replacing of "D" produces "Dail European Parliament and Local Elections onlyail European Parliament and Local Elections only"

 

Any idea what's gone wrong?

 

It also seems to happen for "L" a few times but not everytime?? (Strange)[/code]

Link to comment
Share on other sites

<?php
$rewrote = "";
$handle = fopen("C:\Users\Mike\Desktop\AthyDB.txt", "r+"); // Open file to read it read.

if ($handle) {
while (!feof($handle)) // Loop til end of file.
{
$currentline = fgets($handle, 4096); // Read a line.
$currentline=preg_replace('/^D$/','Dail European Parliament and Local Elections only', $currentline);
$currentline=preg_replace('/^S$/','Post or special arrangement only', $currentline);
$currentline=preg_replace('/^L$/','Local Elections only', $currentline);
$currentline=preg_replace('/^E$/','European Parliament and Local Elections only', $currentline);
$rewrote .= $currentline;
$rewrote .= "\n";
}
file_put_contents('C:\Users\Mike\Desktop\test.txt', $rewrote);
fclose($handle);
}
?>

 

That seems to fix the repeating problem but the code seems to stop replacing the letters after 2,000+

 

Very strange. Any idea why it stops replacing?

Link to comment
Share on other sites

<?php
$test = file_get_contents("C:\Users\Mike\Desktop\AthyDB.txt");
$repl = array(
'D' => 'Dail European Parliament and Local Elections only',
'E' => 'European Parliament and Local Elections only',
'L' => 'Local Elections only',
'S' => 'Post or special arrangement only'
);
$test = explode("\n", $test);
foreach ($test as $k => $v){
$test[$k] = $repl[substr($v,0,1)].substr($v,1);
}
$test = implode("\n", $test);
file_put_contents('C:\Users\Mike\Desktop\test.txt', $test);
?>

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.