Hi, hope someone has a good answer for this 'cos I'll be buggered if I can think of an easy one!

nl2br() // great!

Unless I want to include a formatted list in my text. This gives me loads of unwanted <br>'s!

I guess I could make sure there are no \n's in the html part of my text but that's a bit sloppy and some browsers don't like it I think?

Thanks for your thoughts guys

Nick

    string nl2br (string string)

    Returns string with '<br />' inserted before all newlines.

    If you don't want the <br>'s then don't run the string through it. 🙂

      It really would have been much better if you'd not bothered.
      I'm well aware of how the function works, I was looking for a suggestion on how I might use the function but overcome the effect it has on a piece of inserted HTML, not a silly and unhelpfull comment.

      Nick

        are you trying to say that you have input that is a list like:

        <ol>
        <li>foo</li>
        </ol>

        and you want to run nl2br() on this text but don't want

        <ol><br>
        <li>foo</li><br>
        </ol><br>

        but you want newlines to be made br's in all other cases? is that right?

          I responded in the manner I did because I didn't understand your question. And I'm still not sure.

          nl2br() is mean to format text, if you feed it a string that's an entire html document then yes, you'll get gobbledy gook with a lot of extra <br>'s.

          There's simply not an easy way to make nl2br() only insert <br>'s "only where you want them", if you only want <br>'s in certain places, then you might be better off using ereg_replace() and then tweaking it for your special case.

            Nick, you didn't really say exactly why you were needing to use it in the first place. A little more extensive explanation would probably be in order.

              Here it is....

              this is text that is mean't as the body of a page but in one of the pages I need a list like thes;
              <ul>
              <li>one point</li>
              <li>two points</li>
              </ul>

              and the body text idealy continues after that wiith thregular functionality on nl2br _ I've since found that just not having any \n's in the list works fine, it's just a little undesireable!

              Nick

                Sounds a bit to much like hard work doesn't it, it's for use with templates and in the vast majority of pages there is no need for this.

                Nick

                  But Nick: if all your stuff is so nicely formatted with real html tags, why do you need nl2br() in the first place? And if it's just for the occasional &lt;pre&gt; section, why on earth not apply nl2br() to that first before adding it to the page data?

                    Because the the body text is pulled from a DB field (text) which is normally full of just that, text.
                    It's pulled via a page handler that deals with everything in that database and in this one instance I need to break my own rule and site design to insert some HTML in that text field.
                    Do you think I would be better off writing my articles in HTML, like
                    <p>this is another article etc etc</p>
                    <p> and the paragraphs could, I guess be written like this ( I use forms to add new content)</p>
                    <h4> Extra HTML could then go anywhere?</h4>

                    Maybe that's a better way, I just thought the point of a threee tier approach was to seperate content from code including HTML where possible?

                    I know you've been doing this longer so maybe you can give the benefit of your insight?

                    Thanks

                    Nick

                      So in most cases you want to apply nl2br() to keep the line breaks that are in the text? Why not have a separate column that specifies whether or not the text column contains html?

                        run your html through nl2br then use str_replace or eregi_replace if you want it case insensitive.

                        something like this should work, if you want it to be more flexible of spaces then I'd suggest using eregi_replace():

                        $temp = nl2br($mysqlrow->data);

                        $temp = str_replace('&lt;ul&gt;&lt;br /&gt;', '&lt;ul>', $temp);$temp = str_replace('&lt;/li&gt;&lt;br /&gt;', '&lt;/li&gt;', $temp);

                        This is just how I would do it, but you said your were against eregi_replace before, so do what you want. Hopefully this post'll look ok, if not, hopefully you'll get the idea, a prevview would be nice for these forums. 🙂

                          Write a Reply...