Collapse Whitespace but NOT in <textarea>
I'm using this ...
... in an ob_start function.
$buffer = ereg_replace("[\n\r\t\t+]", "", $buffer);
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?
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.)
PS: Here's the difference in this case between ereg_replace and preg_replace:
$buffer = ereg_replace("[\n\r\t]+", " ", $buffer);
$buffer = preg_replace("/[\n\r\t]+/", " ", $buffer);
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.
You do realize that's part of my forum signature and not directed specifically at you, right?
Originally Posted by robindean
I do indeed LOL
A thought, if you're mainly concerned with newlines/tabs between HTML elements, not within them:
$buffer = <<<EOD
<p>blah blah blah</p>
<p>blah blah blah, too</p>
end of text</textarea>
$buffer = preg_replace('#(?<=>)\s+(?=<)#', '', $buffer);
<p>blah blah blah</p><p>blah blah blah, too</p><textarea name='foo'>Text
end of text</textarea><p>the end</p>
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).
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)