Trimming the file using PHP

Posted on 2005-04-01
Medium Priority
Last Modified: 2008-03-06
How to trim the file using PHP.

The Input file is,
         ;           Zone file for sasikumar.com

ns              A

email           A
                MX      10 email

www               A
               MX   10 mail        

The Output should be,
; Zone file for sasikumar.com

ns A

email A
MX 10 email

www A
MX 10 mail        

There should be only one space between the words.

Sasikumar Natarajan
Question by:isas_n
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 32

Accepted Solution

Batalf earned 500 total points
ID: 13680589


$fh = fopen("textFile.txt","r");
$data = fread($fh,filesize("textFile.txt"));

$data = preg_replace("/(\s)\s+/si","\\1",$data);

echo $data; // With only one space.

LVL 49

Expert Comment

ID: 13680596
Even shorten.

$file = file_get_contents("myfile.zone.txt");
$file = preg_replace("/([\s])[\s]+/","\\1",$file);

No use of having the /i modifier, as you are matching whitespaces...

LVL 32

Expert Comment

ID: 13680621
I know :-) The "i"(case insensitive) flag isn't needed. I put it there by mistake since I so often use it in regexp.


should be enough.


Expert Comment

ID: 13688653
The replacement string should be " " not "\\1" as the zone files he is modifying may have tabs and he wanted spaces, and tabs do match in \s, thus Roonaan's example becomes:

$file = file_get_contents( "myfile.zone.txt" );
$file = preg_replace( "/([\s])[\s]+/", " ", $file );
LVL 14

Expert Comment

ID: 15983484
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Batalf {http:#13680589}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question