SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What should I replace the nl2br with?

    Hello there,

    Up to now I've always programmed my own CMS with a nl2br() functionality - so that when you type an article it would change all the new lines with breaks.

    However there is a problem if you're trying to keep to w3c standards;

    If you write a list using the <ul> or <ol> command put a list it will create an w3c error.

    Example;
    <ul>
    <li>Apples</li>
    <li>Oranges</li>
    <li>Pears</li>
    </ul>
    If I type the above into my textarea and press enter I know that the nl2br() command will physically put in a line-break after each </li>

    This breaks the w3c guidelines and stuff.

    What should I replace the nl2br() command with, I want it to understand that if there's a </li> it shouldn't really be a break or something.

    Wordpress handles it, but I can't figure out how it does it.

    I hope you can help.

  2. #2
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Hope you like it so with input in $ori:
    PHP Code:
    $out '';
    $arr preg_split('#<ul>|</ul>#'$ori);
    for (
    $i 0$i count($arr); $i++) {
        if (
    $i 2) {
            
    $out .= '<ul>'.$arr[$i].'</ul>';
        } else {
            
    $out .= nl2br($arr[$i]);
        }

    I think it's charming
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    London, UK.
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had the same issue.
    I just add some str_replace calls like so.
    PHP Code:
     $txt str_replace('<ul><br />''<ul>'$txt);
     
    $txt str_replace('</ul><br />''</ul>'$txt);
     
    $txt str_replace('</li><br />''</li>'$txt); 
    Simple, yet effective.

    David
    Do you fear the obselescence of the
    metanarrative apparatus of legitimation?

  4. #4
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Effective ?
    I would like to bench Especially when no <ul>
    Should we make a race ?
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    London, UK.
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm simply providing another way of achieving an end result.
    No need to be pedantic.

    If you want to test it, by all means do.
    Do you fear the obselescence of the
    metanarrative apparatus of legitimation?

  6. #6
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excuse me an my poor english.
    I understood "efficient" what is not exactly the same as "effective"
    And you're right resource usage difference should not be sensible.
    CMS are loosing time elsewhere.
    What counts is to succeed to make it
    Sorry,
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    London, UK.
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed
    Do you fear the obselescence of the
    metanarrative apparatus of legitimation?

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by worchyld
    Hello there,

    Up to now I've always programmed my own CMS with a nl2br() functionality - so that when you type an article it would change all the new lines with breaks.

    However there is a problem if you're trying to keep to w3c standards;

    If you write a list using the <ul> or <ol> command put a list it will create an w3c error.
    I don't think applying nl2br to html is a right way. This may lead to severe problems with your output.

    Quote Originally Posted by php at keithtyler dot com in php manual
    Take extreme care with nl2br(). It is a simple replacement function -- apparently equivalent to preg_replace("\n","<br \>\n").
    It should not be used on input from HTML textareas, unless all HTML tags are stripped from the input first. nl2br() does not do anything special to newlines that occur within HTML elements (such as the <a> anchor tag).
    nl2br can also screw up <script> and <style> elements. So think twice before using it!

  9. #9
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi SteroFrog,
    I don't think that worchyld treats full html with that. I think he has only a few authorized html tag in it.
    So, that could do the job here as he must first sanitize dangerous tags.
    +
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  10. #10
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all for your help.

    It should not be used on input from HTML textareas, unless all HTML tags are stripped from the input first
    I knew that there was some "brewhaha" about using nl2br() because it screwed up html or wasn't w3c valid or screwed up something somewhere at sometime.

    I even read the nl2br() is defunct and a new function had to be custom written for it so that it could be understood -- something to do with different doctypes or something or unix format or something, I cannot remember.

    Are there inherit problems with the nl2br() function? What should, if I'm making a CMS with text-area, replace it with?

    Or, let's assume there is HTML or if its a WYSIWYG enhanced text box -- what should I do?

    Thank you very much!

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, as a workaround you may use something like this:
    PHP Code:
    $html "
    <a
        href='no'
        target='breaks'
        onclick='please'>line1
        line2
        line3
    </a>

    <script>
        var line1;
        var line2;
    </script>

    <div class=hello>WE
        LOVE
        BREAKS
    </div>
    "
    ;

    $allowed_tags "a|p|div|span|h[1-5]";
    $breaks preg_replace_callback(
        
    "~(<($allowed_tags)(.*?)>)(.+?)(</\\2>)~si"
        
    create_function('$m',
            
    'return $m[1] . nl2br($m[4]) . $m[5];'),
        
    $html);
    echo 
    "<xmp>$breaks</xmp>"
    This inserts <br> only where they make sense.

  12. #12
    SitePoint Member
    Join Date
    Jul 2004
    Location
    The United of Kingdom
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  13. #13
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help!


Bookmarks

Posting Permissions

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