View Full Version : Linebreaks, PRE tag, and fopen()/fread()

10-10-2004, 03:41 AM
I've been working on a RSS parser for a couple of months now. Everything is peachy so far, except for two issues. I'll discuss one in this thread, and the other one in another thread.

When I parse through and display code from, say, a <description> tag, some feeds contain <pre> tags that are used to display code blocks on the syndicated site.

The problem is that when I'm reading and displaying the feed, the <pre> tag gets obeyed, but all of the content appears to be on a single line. Is there any way to make fopen() and/or fread() repect linebreaks in the original content?

10-10-2004, 07:18 AM
you could try doing a nl2br().
or you could eregi_replace("\n\r","<br/>",$line);


10-10-2004, 07:03 PM
Thanks for giving me some direction. I found this at PHP.net:

function nl2br_pre($string, $wrap = 40) {
$string = nl2br($string);
preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/", $string, $pre1);

for ($x = 0; $x < count($pre1[0]); $x++) {
$pre2[$x] = preg_replace("/\s*<br[^>]*?>\s*/", "", $pre1[0][$x]);
$pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)(?!\n)/", "$1\n", $pre2[$x]);
$pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/";

return preg_replace($pre1[0], $pre2, $string);

This will keep the line breaks as-is.

Thanks! :thumbsup: