simple php script to remove newline in text file

This is a simple php script ( as reference) created by me to remove newline @ carriage return in my text file and replace it with pipe “|” .

php
here is the code:


<?php

/* @author : hafeez */

$baca= fopen(“contoh.txt”,“r”);

$tulis = fopen(“output.txt”,“w+”);

$text = fread($read, filesize(“contoh.txt”));

$text = str_replace(“\r\n”,“|”,$text);

echo $text;

fwrite($tulis,$text);

fclose($baca);

fclose($tulis);

?>

contoh.txt :

this is line one
ini line kedua
three


output.txt:

this is line one|ini line kedua|three

ok, enjoy ^_^

Advertisements

11 thoughts on “simple php script to remove newline in text file

  1. myapit Post author

    could i have the example of the text file…
    b’coz currently i only able to manipulate unformatted text file ( e.g: file saved from notepad not wordpad)..

    but if i could change the font of the text file, i will post it here..
    thanks.

    Reply
  2. phpdevmd

    Thanks for your tip. If you have a string which ends with new line, then that code won’t work.

    Use this to replace new lines for sure:
    $text = str_replace(array(“\r\n”,”\n”,”\r”),”|”,$text);

    Reply
  3. Balister

    Hi!

    I was facing problem in displaying tool tip in javascript due to new line in content.

    You have solved my problem in displaying tool tip in javascript.

    Thanks
    Balister, India

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s