Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Linebreaks, PRE tag, and fopen()/fread()

    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?

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    you could try doing a nl2br().
    or you could eregi_replace("\n\r","<br/>",$line);

    -celt

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for giving me some direction. I found this at PHP.net:
    PHP Code:
    <?php
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •