Log in

View Full Version : grammar



james438
07-12-2007, 04:41 PM
hi, how would you say the following in a gramatically correct way:

"I would like to do a match for every three chained occurances of spaces in a row or more and replace them with just one so as to avoid needless whitespace?"

I know how to do it, but I was wondering what the best way would be to say that if I were a poster asking help from a moderator and wish to be most easily understood.

Thanks :P

djr33
07-12-2007, 04:47 PM
$str = str_replace($str,' ',' ');

We wall speak code here ;)

"How do I replace sets of 3 spaces with a single space?", or "How do I remove excess spaces?"

james438
07-12-2007, 05:02 PM
cool, thanks for the three answers! although I think I would have done something like
$summary=preg_replace('/((\040){2,})/'," ",$summary);
instead :)

mwinter
07-12-2007, 07:22 PM
hi, how would you say the following in a gramatically correct way:

"I would like to do a match for every three chained occurances of spaces in a row or more and replace them with just one so as to avoid needless whitespace?"

How do I collapse three or more consecutive white space characters to a single space?



[...] I think I would have done something like
$summary=preg_replace('/((\040){2,})/'," ",$summary);

Better would be:



$summary = preg_replace('/\w{3,}/', ' ', $summary);

assuming you really meant three white space characters - two, as you coded, would have more sensible. However, that would remove line terminators, so an alternative would be to replace the character class escape \w with the negative character class [^\W\r\n].

james438
07-12-2007, 09:32 PM
This is beginning to get into PHP themed thread ;) but you are right, I meant to use a 3 instead of a two in my example. I replaced it with  , because when I use ' ' like in your example my PHP seems to automatically condense sets of spaces to just one. I found a reason to be able to keep that extra bit of whitespace so I used this line of code:


$summary[$i]=preg_replace('/((\040){2,2})/',"  ",$summary[$i]);

The reason being that it will replace every set of two spaces as opposed to any ol' whitespace with ' &nbsp' which is then displayed as two spaces occurring together. If there are 3 spaces the first two will be replaced and the third will just be tacked onto the end. Thus all spaces are now displayed from a document.

I am still new to the PCRE aspect of PHP and line terminators is a new term for me, unless you are talking about newline and carriage returns. I was accidentally removing line terminators in my displayed documents and it was getting annoying, which is why I used the above code.

At this point it might be good to split the topic, but... could you use [^\W\r\n] in a sentence? If I am reading it right the script would recognize and replace all whitespace except for \r\n. Is that correct?