www.webdeveloper.com
Results 1 to 8 of 8

Thread: Collapse Whitespace but NOT in <textarea>

  1. #1
    Join Date
    Dec 2005
    Posts
    492

    Collapse Whitespace but NOT in <textarea>

    I'm using this ...

    Code:
    $buffer = ereg_replace("[\n\r\t\t+]", "", $buffer);
    ... in an ob_start function.

    I realize ereg_replace is depricated but preg_replace is still difficult for me. I'll switch to that eventually.

    My problem is that when I load external data into a <textarea> tag, it's collapsed (as is to be expected).

    How do I execute the above action but have it NOT effect anything inside of a <textarea> tag?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,252
    I can't think of anything other than a three-pass operation that would first change any of those characters within textarea tags to some sort of place-holders, then doing the global replace, and then replacing those first-pass place-holders with the original characters -- at which point you are probably spending more time doing all that processing than the millisecond or two of time you saved in HTTP request time by stripping out that white space. (In fact, it would be interesting to put some metering on it as is and see if you are actually saving time with just the single-pass replacement you're doing now.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,252
    PS: Here's the difference in this case between ereg_replace and preg_replace:
    PHP Code:
    // EREG:
    $buffer ereg_replace("[\n\r\t]+"" "$buffer);

    // PREG:
    $buffer preg_replace("/[\n\r\t]+/"" "$buffer); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Dec 2005
    Posts
    492
    Thanks for the simple preg_replace tip.

    My reason for this question isn't to correct my own code but to correct code submitted by users in what is an account based system.

    At the same time, I don't want to edit code within the textarea.

    Sure it's a riddle wrapped in an enigma but nonetheless my question remains.

    By the way, the hope for a simple answer doesn't guarantee a non-thinker is asking the question.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,252
    Quote Originally Posted by robindean View Post
    ...

    By the way, the hope for a simple answer doesn't guarantee a non-thinker is asking the question.
    You do realize that's part of my forum signature and not directed specifically at you, right?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Dec 2005
    Posts
    492
    I do indeed LOL

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,252
    A thought, if you're mainly concerned with newlines/tabs between HTML elements, not within them:
    PHP Code:
    <?php
    $buffer 
    = <<<EOD
    <p>blah blah blah</p>
       <p>blah blah blah, too</p>
          <textarea name='foo'>Text
    text text
       indented text
    end of text</textarea>
    <p>the end</p>
    EOD;

    $buffer preg_replace('#(?<=>)\s+(?=<)#'''$buffer);
    echo 
    "<pre>".htmlspecialchars($buffer)."</pre>";
    Output:
    Code:
    <p>blah blah blah</p><p>blah blah blah, too</p><textarea name='foo'>Text
    text text
       indented text
    end of text</textarea><p>the end</p>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Dec 2005
    Posts
    492
    Thank you! Excellent suggestion. This pointed me in many interesting directions (some of which I'll abuse for a day, others I'll keep LOL).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles