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
https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/
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...
?>

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...
?>

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

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...
?>

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

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.