removing carriage returns/line feeds/line breaks
Posted by Stooshie (Stooshie), 13 March 2006Hi there,
I am currently working on code to publish a flat xhtml page by parsing a second php page that takes content from a database (the flat page loads quicker for the public).
Anyway, I have the following code to reduce the size of the page a little.
However, the output is all on one line and older browsers may struggle with this.
What I would ideally like is to replace 2 or more carriage returns, line feeds or line breaks with 1 line break.
I've tried things like:
but to no avail.
I've been struggling with this regular expression for a while and my head is now sore!! .
ps. I know the second regular expression to remove tabs can probably be added to the first one, but I was trying to get the first one working before I attempt that.
Posted by admin (Graham Ellis), 13 March 2006At first glance, I would replace all the new lines and returns with a tab and then replace all groups of 2 or more tabs with a new line. That way, you're left with single tabs or single new lines.
Code would look something like this:
$line = ereg_replace("[\n\r]", "\t", $line);
$line = ereg_replace("\t\t+", "\n", $line);
Comment by Graham Ellis (published 2011-02-18)
I agree that ereg should be replaced by preg functions. Note that this page is a blog archive dating from six years ago .... I'm adding this note as an alert to people reading the page, rather than going back and altering history. [#3890]
Comment by Interesting Articles (published 2011-02-18) Suggested link.
Avoid this at all costs. ereg() is depreciated and is being removed from php, use preg_match instead. [#3887]
Comment by Anon (published 2011-01-16)
Good information, but the code example font size is too small [#3826]
You can Add a comment or ranking or edit your own comments
Average page ranking - 3.3
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho