Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
removing carriage returns/line feeds/line breaks

Posted by Stooshie (Stooshie), 13 March 2006
Hi 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.
Code:
                       foreach($fin as $num => $line)
                       {
                             $line = ereg_replace("/\n\r|\r\n|\n|\r/", "", $line);
                             $line = preg_replace("/\t/", "", $line);
                             fwrite($fout, $line);
                       }

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:
Code:
$line = ereg_replace("/[\n|\r|\n\r|\r\n]{2,}/", "\n", $line);

but to no avail.

I've been struggling with this regular expression for a while and my head is now sore!! .

Any ideas.

Thanks.

Andrew.

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 2006
At 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);




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho