Results 1 to 5 of 5

Thread: grammar

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default grammar

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $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?"
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    cool, thanks for the three answers! although I think I would have done something like
    $summary=preg_replace('/((\040){2,})/',"&nbsp;",$summary);
    instead

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by james438 View Post
    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?

    Quote Originally Posted by james438 View Post
    [...] I think I would have done something like
    $summary=preg_replace('/((\040){2,})/',"&nbsp;",$summary);
    Better would be:

    PHP Code:
    $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].
    Last edited by mwinter; 07-12-2007 at 07:27 PM.
    Mike

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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 &nbsp;, 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:

    PHP Code:
    $summary[$i]=preg_replace('/((\040){2,2})/',"&nbsp;&nbsp;",$summary[$i]); 
    The reason being that it will replace every set of two spaces as opposed to any ol' whitespace with '&nbsp;&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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •