?
Solved

Trimming the file using PHP

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

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

ns              A       12.168.1.24

email           A       12.168.1.24
                MX      10 email

www               A   61.12.281.126
               MX   10 mail        


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

ns A 12.168.1.24

email A 12.168.1.24
MX 10 email

www A 61.12.281.126
MX 10 mail        

There should be only one space between the words.

Regards,
Sasikumar Natarajan
Chennai
0
Comment
Question by:isas_n
[X]
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
6 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 500 total points
ID: 13680589
try

<?php

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

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

echo $data; // With only one space.

?>
0
 
LVL 49

Expert Comment

by:Roonaan
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...

-r-
0
 
LVL 32

Expert Comment

by:Batalf
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.

/(\s)\s+/s

should be enough.

0
 
LVL 3

Expert Comment

by:lokip
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 );
0
 
LVL 14

Expert Comment

by:huji
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.

Huji
EE Cleanup Volunteer
0

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