Jump to content

[SOLVED] New line after keyword??


mikebyrne

Recommended Posts

I currently have code thats trimming spaces after commas. Is it possible to modify this code to have a new line after the words "Co.Kildare" as this will be the last word in every line.

 

The code is

 

<?php
$txt = file('/path to filename/filename'); // choose correct path location and file
foreach ($txt as $i=>$l)
     $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n";
file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed...
?>

 

 

Link to comment
Share on other sites

Im not sure if this will work but you can give it a try:

 

<?php
$txt = file('/path to filename/filename'); // choose correct path location and file
foreach ($txt as $i=>$l)
     $txt[$i] = preg_replace('/Co.Kildare/', 'Co.Kildare'."\n", $txt[$i]);
     $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n";
file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed...
?>

Link to comment
Share on other sites

Hmm, maybe:

 

<?php
$txt = file('/path to filename/filename'); // choose correct path location and file
foreach ($txt as $i=>$l)
     $txt[$i] = preg_replace('/Co.Kildare/', 'Co.Kildare'."\r\n", $txt[$i]);
     $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n";
file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed...
?>

Link to comment
Share on other sites

No that just puts an extra line inbetween them. Here's what the last three lines look like

 

L  ,2811,  Axxxxx, Dxxxxx,  22  Dun Bxxn, Blexxx Roxxxd, Axxxhy, Co.Kildare

 

L  ,2812,  Axxxxi, Lxxxcia,  22  Dun Brxxn, Blexxxxh Roxxxd, Axxy, Co.Kildare

 

L,2813,Achxxxxxritei,Mxxx,22  Dun Brxxxn,Blxxxxch Rd,Axxxy,Co.Kildare

 

The last line is in the correct format im looking for

Link to comment
Share on other sites

I am guessing at this point but try swapping the two lines:

 

<?php
$txt = file('/path to filename/filename'); // choose correct path location and file
foreach ($txt as $i=>$l)
     $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n";
     $txt[$i] = preg_replace('/Co.Kildare/', 'Co.Kildare'."\r\n", $txt[$i]);
file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed...
?>

Link to comment
Share on other sites

I just tried out the code I gave you in the previous thread. I used xampp on WindowsXP.

<?php
$fn = 'test.txt';
$txt = file($fn); // choose correct path location and file
foreach ($txt as $i=>$l)
     $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n";
file_put_contents($fn, implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed...
?>

on this file:

L  ,2486,  Sam, Smith,,  xx Castle Park, Atby, Co.Kil
L  ,2486,  Sam, Smith,,  xx Castle Park, Atby, Co.dare
L  ,2486,  Sam, Smith,,  xx Castle Park, Atby, Co.Kildare
  ,2486,  Sam, Smith,,  xx Castle Park, Atby, Co.Kil
L  ,2486,  Sam, Smith,,  xx Castle Park, Atby, Co.dare
L  ,2486,  Sam, Smith,,  xx Castle Park, Atby, Co.Kildare

The results when viewed in notepad show on one line.

 

When viewed in wordpad they show on separate lines.

 

When you replace

<?php
$txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n";
?>

with

<?php
$txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\r\n";
?>

the file shows correctly with both applications.

 

Ken

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.