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
https://forums.phpfreaks.com/topic/153175-solved-strange-values-repeating/
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?

<?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);
?>

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.