View Full Version : Deleting 2 or more spaces with preg_replace() ?

12-29-2006, 11:06 PM
I'm still a RegEx n00b & can't work out how to do this; I tried the below but it didn't seem to work. :(

$line2 = preg_replace('/ +/', ' ', $line);


12-29-2006, 11:30 PM
I believe your regex is okay, you might want to check if you are assigning variables appropriatly. Note that even if a line of text contains many spaces, most web browsers will only render a single space. I still think your code could be a assignment error though. Never the less, here is some code that will work:

$l = 'This is a line of text.';
$l = preg_replace('# +#', ' ', $l);
echo $l;

12-29-2006, 11:45 PM
Hmmm...... well here is my full code so you can see:

$file = file('org.txt');

foreach ($file as $line) {

$line = trim($line);

$line2 = preg_replace('/ */', ' ', $line);

echo "$line<br>$line2<p>\n";



The "echo ()" & the "exit()" are just there for testing purposes. As you say, yeah... the browser display is fine; but when I look in the source both outputs are still the same & $line2 still contains big spaces.

Here is an example line from "org.txt"..

activitedechezsoi.org 2006-12-31 765 0 0 0 0 11 2005


12-30-2006, 12:08 AM
You now have two spaces in your regex pattern, and a *. You need one space and a + as per my example. Try using hash's to serpate your pattern too instead of forward slashes. It could be the fact that you are using file. Not sure. try file_get_contents($fileName) instead.

Also, you cannot use foreach to itterate over a string. You should do this before if you want to use a foreach:

$file = explode("\n", $file);

This also presumes your file is in unix format.

12-30-2006, 12:25 AM
Sorry...... that wa sjust another way I was testing it cause the method I posted above didn't work.

As for the "foreach()"; I am using that to iterate over each line of the file & why file_get_contents() won't work as it won't sort it into lines; only one complete string.

12-30-2006, 12:39 AM
the foreach might be your problem, perhaps post the real code that doesn't work, if the following doesn't help?

When you use 'as $something' in the foreach loop, $something isn't a reference to the array-value, so making changes to it will 'be lost' once the loop is finished.
An example

$array = array('a','b','c','d');
foreach($array as $item) {
echo $array[1]; //will output 'b', not 'x'

one solution:

$array = array('a','b','c','d');
foreach($array as $key=>$value) {
echo $array[1]; //will echo 'x' now

by using the $key=>$value optons with foreach, it allows you to refer back to the actual array, and make changes to it.

Is this what's happening with your loop?

12-30-2006, 12:54 AM

preg_replace('/\s+/',' ', $text);

12-30-2006, 02:36 AM
Ah, I feel like a complete arse, I can't believe I didn't spot it! What GJay said was correct. foreach assigns the seccond argument by copying, not by reference. His example is correct and should solve your problem.

12-30-2006, 12:38 PM

preg_replace('/\s+/',' ', $text);
Thank you so much..... tried everything but that was the only thing that worked! :)

12-31-2006, 12:49 AM
I had a feeling that doing something slightly different from what you asked will do the trick. ;)

12-31-2006, 02:51 PM