Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

Comment: "I agree that ereg should be replaced by preg functions. ..."
Visitor Ranking 3.3 (5=excellent, 1=poor)

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

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