...

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



cyphix
12-29-2006, 10: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);


Thanks!

xconspirisist
12-29-2006, 10: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;

cyphix
12-29-2006, 10: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";

exit();

}


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


Cheers!

xconspirisist
12-29-2006, 11:08 PM
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.

cyphix
12-29-2006, 11:25 PM
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.

GJay
12-29-2006, 11:39 PM
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) {
$item='xyz';
}
echo $array[1]; //will output 'b', not 'x'


one solution:


$array = array('a','b','c','d');
foreach($array as $key=>$value) {
$array[$key]='x';
}
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?

marek_mar
12-29-2006, 11:54 PM
Try


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

xconspirisist
12-30-2006, 01: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.

cyphix
12-30-2006, 11:38 AM
Try


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

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

Nicklas
12-31-2006, 01:51 PM
nv...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum