Jump to content

[SOLVED] Replacing single letters with sentence?


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)

 

Is it possible to code this instead of manually replacing them??

 

Ideally I would like the output to pass into a new file

Link to comment
Share on other sites

preg_replace('^D','Dail European Parliament and Local Elections only');
preg_replace('^S','Post or special arrangement only');
preg_replace('^L','Local Elections only');
preg_replace('^E','European Parliament and Local Elections only');

 

That might work (my first attempt at regex) but if you're parsing the CSV file then just replace the first element.

switch ($element[0]) {
  case 'D':$element[0]='Dail European Parliament and Local Elections only';break;
  case 'S':$element[0]='Post or special arrangement only';break;
  //...and so-on
}

Link to comment
Share on other sites

The thing is you are asking us to code the whole thing for you without showing any attempt at doing it yourself.

 

You will not be given more help unless:

a) you post in the freelance section

b) you atleast attempt to wrote it yourself then come back when you have a problem.

 

Matt

Link to comment
Share on other sites

I've tried with the following code

 

<?php
$fields=file('C:\Users\Mike\Desktop\AthyDB.txt');
    foreach($fields as $i=>$field)
    {
	$field=preg_replace('^D','Dail European Parliament and Local Elections only');
	$field=preg_replace('^S','Post or special arrangement only');
	$field=preg_replace('^L','Local Elections only');
	$field=preg_replace('^E','European Parliament and Local Elections only');
       $fields[$i]=$field;
    }
    file_put_contents('C:\Users\Mike\Desktop\test.txt',implode("\r",$fields));
?>

 

 

 

 

but im getting the following error

 

 

Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 5

 

Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 6

 

Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 7

 

Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 8

Link to comment
Share on other sites

basically you have to pass it the string you want it to run on.

without showing any attempt at doing it yourself

(didnt mean to sound harsh jut explaining the reason why you were not getiing any more help, no code = no help as a general rule)

 

but anyway try this

 

differnt to yours cause its not using an array but hey if it works

 

<?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');
fclose($handle);
}

?>

 

Please make a backup of your file first as this is untested and may create unintention bugs.

 

Link to comment
Share on other sites

with ths code

 

<?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');
fclose($handle);
}
?>

 

Im getting a parse error "Parse error: parse error in C:\xampp\htdocs\letters.php on line 10"

 

Line 10 is $currentline=preg_replace('^S','Post or special arrangement only', $currentline));

 

Link to comment
Share on other sites

You've got two brackets at the end of three of those lines when you should only have one - should be like this:

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

Link to comment
Share on other sites

Sorry! Good spot

 

Im now getting the error

 

Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 9

 

Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 10

 

Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 11

 

Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 12

 

Should the code  be '^S,' or something??  (No I just tried it!)

Link to comment
Share on other sites

That seems to have fixed that issue but im now getting the error

 

Warning: file_put_contents() expects at least 2 parameters, 1 given in C:\xampp\htdocs\letters.php on line 16

 

Line 16 is

 

file_put_contents('C:\Users\Mike\Desktop\test.txt');

Link to comment
Share on other sites

I've changed it to

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

 

And it is now working! Thanks for all the help!!

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.