?
Solved

change multiple spaces to one space - php

Posted on 2010-08-28
8
Medium Priority
?
562 Views
Last Modified: 2012-06-27
Hi

This follows on my previous question here:
http://tinyurl.com/35k9wnq 

I need to replace random number of spaces in each line with one space so that a line like this:
192\.168\.99\.44            25h/day
becomes
192\.168\.99\.44 25h/day.

I am reading the lines into an array with this script:

<?PHP
   $file_handle = fopen("/var/squish/squish.conf", "rb");
     while (!feof($file_handle) ) {
       $line_of_text = fgets($file_handle);
       if (substr($line_of_text,0,1)!='#') {
         $parts = explode(' ', $line_of_text);
         print $parts[0] . $parts[1]. "<BR>";
         }
  }
fclose($file_handle);
?>

and because I am splitting the lines with a space the random numbers make things go haywire.
0
Comment
Question by:QuintusSmit
[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
8 Comments
 
LVL 7

Accepted Solution

by:
mcuk_storm earned 300 total points
ID: 33550158
You can use preg_replace to use a regular expression to match one or more spaces and replace them with one:
$result = preg_replace("/[ ]+/"," ",$subject);
0
 
LVL 12

Assisted Solution

by:zappafan2k2
zappafan2k2 earned 100 total points
ID: 33550196
The PHP file function reads a file into an array based on line breaks for you.

Here's how I would do it.

<?php
$myArray = file("/var/squish/squish.conf");
foreach ($myArray as $line) {
  $line = preg_replace('/\s+/', ' ', $line);
  if (!preg_match('/^#/', $line) {
    $parts = preg_split('/ /', $line);
    echo implode('<whateverdelimiteryouwant>', $parts) ."<br>";
    }
  }
?>

Open in new window

0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33550538
Regular expressions are terribly slow.

use staigforward solution.

$n will be the new string.

$c=file_get_contents('/var/squish/squish.conf');
$n='';

$prev=false;
$size=strlen($c);
for($i=0; $i<$size; ++$i) {
	if ($c[$i]==' ') {
		if (!$prev) {
			$prev=true;
			$n.=' ';
		}
	} else {
		$n.=$c[$i];
		$prev=false;
	}
}

Open in new window

0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33550603
I don't think you do regexp's justice, I have just compared the following two script:

1:
<?php
$c=file_get_contents('testdata');
$n='';

$prev=false;
$size=strlen($c);
for($i=0; $i<$size; ++$i) {
        if ($c[$i]==' ') {
                if (!$prev) {
                        $prev=true;
                        $n.=' ';
                }
        } else {
                $n.=$c[$i];
                $prev=false;
        }
}
echo $n;
?>


2:
<?php

$n='';
$data = file('testdata');
foreach($data as $line) {
        $n .= preg_replace('/[ ]+/',' ',$line);
}
echo $n;
?>


Script 1 execution:
time php < test.php | md5sum
989438e5c3647807a7b552c7212515c4  -

real    0m22.861s
user    0m22.378s
sys     0m0.164s


Script 2 execution:
time php < regexp.php | md5sum  
989438e5c3647807a7b552c7212515c4  -

real    0m0.317s
user    0m0.208s
sys     0m0.105s


The test dataset was a 10000 row file, each row containing two segments of data separated by the same number of spaces as the record number. To rule out the difference between file and file_get_contents i ran tests with just these statements in and file is about 0.1sec slower than get_contents. The MD5s show that the outputs are the same
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 33550844
REGEX is the correct way to do this.  "Slow" is irrelevant in the instant context.  You would need to do hundreds of millions of REGEX before you could even notice a difference, and the time spent on this question will never be recovered in "real life."

Please see example #5 of the man page here:
http://us2.php.net/manual/en/function.preg-replace.php

We have that example in the PHP/net web site because it's the right way to do it! ~Ray
Example #5 Strip whitespace

This example strips excess whitespace from a string.
<?php
$str = 'foo   o';
$str = preg_replace('/\s\s+/', ' ', $str);
// This will be 'foo o' now
echo $str;

Open in new window

0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33552319
@mcuk_storm:

Hm, that is probably the reason because regex module is written in C and can execute machine code directly, but generally, my approach is faster.

My mistake, I guess that is my C++ stereotype that isn't true for PHP.
0
 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33552324
@Rok-Kralj
Regexp can be very inefficient depending on the complexity of the statements and how they are written (though usually the alternative code is just as complex) but for smaller statements it is generally pretty well optimised. Unfortunately it does seem to have the stigma of being slow but its not always the case.
0
 
LVL 1

Author Comment

by:QuintusSmit
ID: 33552617
tx for all the responses - regular expressions would be fine as this file will never have more than 150 lines
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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