SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,930
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Need Returns in HTML Source

    I am building a function that wraps <p></p> around each paragraph entered into a TextArea.

    It is pretty close to working, except that when I do View---> Source, everything is on one super long line?!

    How do I put carriage returns or whatever they are called in the Source?

    Here is my code...
    PHP Code:
    function nl2p($string$line_breaks true) {
        
    // Remove existing HTML formatting to avoid double tags.
        
    $string str_replace(array('<p>''</p>''<br>''<br/>'), ''$string);

        
    // Replace Carriage Return with Empty String.
        // Replace multiple Newlines with closing & opening paragraph tags.
        // Replace single Newline with break tag.
        
    if ($line_breaks == true) {
            return 
    '<p>'.preg_replace(array("#\r#","#\n{2,}#""#\n#"), array("","</p><p>""<br />"), $string).'</p>';
        }else{
            return 
    '<p>'.preg_replace("/\n/""</p>\n<p>"trim($string)).'</p>';
        }
    }

    echo 
    nl2p($textTRUE); 

    If I add \n into the line below with single quotes, then it just adds a visible \n to the source...
    return '<p>'.preg_replace(array("#\r#","#\n{2,}#", "#\n#"), array('','</p><p>\n', '<br />\n'), $string).'</p>';

    And if I add \n into the line with double quotes, then it ends up adding extra carriage returns in my output...
    return '<p>'.preg_replace(array("#\r#","#\n{2,}#", "#\n#"), array("","</p><p>\n", "<br />\n"), $string).'</p>';

    I think my preg_replace is the culprit...


    Debbie

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    NVM
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,930
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    NVM
    Huh????


    Debbie

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Why put carriage returns in the code any ways? What doe it matter if it is all on one line? All the debugging tools I've used concerning HTML format the HTML into a proper tree, Firebug one such example.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,930
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Why put carriage returns in the code any ways? What doe it matter if it is all on one line? All the debugging tools I've used concerning HTML format the HTML into a proper tree, Firebug one such example.
    You are right in that I am being anal-retentive here, but why not shoot for the stars?

    Just to clarify, here is what I see now...
    Attachment 59794


    And this is how it should normally look...
    Attachment 59795


    So can that minor issue be fixed?

    Thanks,


    Debbie

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    One of the differences between ' ' and " " in PHP is which escape characters they recognise. Between ' ' the only escape character recognised is \'

    To have the \n escape recognised you must wrap it in " ".
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,930
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    One of the differences between ' ' and " " in PHP is which escape characters they recognise. Between ' ' the only escape character recognised is \'

    To have the \n escape recognised you must wrap it in " ".
    But when I do that in my code it is also inserting carriage returns in the output?!

    Here is a function I wrote that seems to work pretty well, but View--->Source yields everything on one line which is really ugly and a pain.

    (I guess viewing the source in a "DOM Tree" in some editors would fix that, but I am old-school and want it working in the browser's source as well.)

    Here is my function...
    PHP Code:
    $text="I decided to start my own business because I want to be my own boss!



    My boss is a jerk and never appreciates anything that I do for him, so why put up with the abuse?!  He takes me for granted and doesn't appreciate all of my talents.

    Running my own business will give me a chance to do things as I see fit...

    Line One
    Line Two
    Line Three
    Line Four



    Line Eight

    Line Ten"
    ;


    $text2 htmlentities($textENT_QUOTES);


    function 
    nl2p($string$line_breaks true) {
        
    // Remove existing HTML formatting to avoid double tags.
        //^    $string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string);

        // Replace Carriage Return with Empty String.
        // Replace multiple Newlines with closing & opening paragraph tags.
        // Replace single Newline with break tag.
        
    if ($line_breaks == true) {
            return 
    '<p>'.preg_replace(array("#\r#""#\n{3,}#""#\n{2}#""#\n#"), array("""</p><br /><p>""</p><p>""<br />\n"), $string).'</p>';
    //        return '<p>'.preg_replace(array("#\r#","#\n{2,}#", "#\n#"), array("","</p><p>", "<br />\n"), $string).'</p>';
        
    }else{
            return 
    '<p>'.preg_replace("#\n#""</p><p>"trim($string)).'</p>';
        }
    }

    echo 
    nl2p($text2TRUE); 
    If you can help me figure out why I get the View--->Source I do, I'd be grateful!!

    Thanks,



    Debbie

  8. #8
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,313
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Do you mean you want line breaks between the paragraph tags?

    return '<p>'.preg_replace(array("#\r#", "#\n{3,}#", "#\n{2}#", "#\n#"), array("", "</p>\n<br />\n<p>", "</p>\n<p>", "<br />\n"), $string).'</p>';

  9. #9
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,930
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    Do you mean you want line breaks between the paragraph tags?

    return '<p>'.preg_replace(array("#\r#", "#\n{3,}#", "#\n{2}#", "#\n#"), array("", "</p>\n<br />\n<p>", "</p>\n<p>", "<br />\n"), $string).'</p>';
    Do I want each <p> on a separate line in my View--> Source, yes!!

    But do I want those to show up as additional carriage returns in my output, no!! (Unfortunately they do.)

    Try running my code above - with your "fix" - and you will see what I mean...


    Debbie

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,313
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    I ran your code, but I don't see what you mean.

    You may need to post what you see when you view source.

  11. #11
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,930
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    I ran your code, but I don't see what you mean.

    You may need to post what you see when you view source.
    First of all, I am on a MacBook...

    Here is the output of my original code...
    Attachment 59804


    And here is the one mega long line of HTML when I do View--->Source...
    Attachment 59806


    After I add in \n into the code like you suggested...
    if ($line_breaks == true) {
    return '<p>'.preg_replace(array("#\r#", "#\n{3,}#", "#\n{2}#", "#\n#"), array("", "</p>\n<br />\n<p>", "</p>\n<p>", "<br />\n"), $string).'</p>';
    ...then here is how the output changes... (Notice how there are extra carriage returns now.)
    Attachment 59805


    Normally adding a \n in between double quotes would just affect how the HTML is formatted when you View--->Source, but here my Regex is treating it like it is to be output.

    Hope you see what I am saying now. (And remember that I am on a Mac...)

    Thanks,


    Debbie

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    What a waste time of time… If you were working on a paid project your boss would probably slap you (maybe not literally) but yeah. This needs to be done like you need a hole in your head. In fact you page will use up more bandwidth by adding extra characters. I know how much everyone in HTML and CSS forum complains about squeezing every byte of performance out of page so why should this be any different. So long as the server-side code is readable HTML source formatting doesn't matter. Just saying… though if it makes you happy and your not investing any client money it than have fun… I guess.

    Quote Originally Posted by AnthonySterling
    NVM
    +1
    The only code I hate more than my own is everyone else's.

  13. #13
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,313
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Ahh, it's because of the multiple replacements. The third replace changes \n{2} into </p><p>. But if you put a newline in there, <p>\n</p>, then the fourth replacement changes that newline into a <br>. Trying to stagger the replacements to get the right output is going to be tricky. It might be easier with a callback...

    PHP Code:
    // warning: untested, and requires >= PHP 5.3

    $string preg_replace('/\r/'''$string);
    $string preg_replace_callback('/\n+/', function ($matches) {
        if (
    strlen($matches[0]) >= 3) {
            return 
    "</p>\n<br />\n<p>";
        } elseif (
    strlen($matches[0]) >= 2) {
            return 
    "</p>\n<p>";
        } else {
            return 
    "<br />\n";
        }
    }, 
    $string
    Though, it would probably be a lot easier just to use the standard nl2br. I'm pretty sure screen readers and search engines will still read it just the same, and it'll make your life a lot easier.

  14. #14
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If you care so much about the HTML's output formatting...
    http://us3.php.net/tidy

    PHP Code:
    <?php ob_start(); # This is important...

    // Your code goes here...

    $super_useless_output ob_get_clean();

    $tidy_options = array( # See http://tidy.sourceforge.net/docs/quickref.html
      
    'clean' => true,
      
    'char-encoding' => 'utf8',
      
    'output-html' => true,
      
    'doctype' => 'strict',
      
    'bare' => true,
      
    'fix-uri' => true,
      
    'indent' => true,
      
    'indent-spaces' => 4,
      
    'tab-size' => 4,
      
    'wrap' => 0,
    );

    $tidy = new Tidy();
    $tidy->parseString$super_useless_out$tidy_options'utf8' );
    $tidy->cleanRepair();

    echo 
    $tidy?>
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •