Converting certain newlines to <br />s

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Finke

    Converting certain newlines to <br />s

    I have a site wherein the clients can update the individual pages by editing
    the text of that page in a <textarea>. Then, when the page is displayed,
    all of the newlines (\n) are converted to HTML line breaks (<br />).
    However, I have come across some instances where I do not want to convert
    the newlines, such as the following:

    <table>
    <tr>
    ....
    </tr>
    </table>

    Obviously, I would not want this to be

    <table><br />
    <tr><br />
    ....
    </tr><br />
    </table><br />

    Can anyone suggest a method for achieving this? I've tried different
    mixtures of regular expressions, but my skills with those are just not up to
    par.

    Christopher Finke


  • somaBoy MX

    #2
    Re: Converting certain newlines to &lt;br /&gt;s


    "Christophe r Finke" <christopherfin ke@hotmail.com> wrote...

    : Can anyone suggest a method for achieving this? I've tried different
    : mixtures of regular expressions, but my skills with those are just not up
    to
    : par.

    Christopher, first off it's never a good idea to allow people to enter html
    code in forms of a content management service. You can allow them to insert
    simple tags like <i>,<b> and <u> but if you're allowing them to create
    things like tables it could seriously screw up the design of the site.

    A practical solution for your problem would be to do a str_replace on the
    text before you store it. For example:

    $remove = array("<table>\ n", "<table>\r" , "<table>\r\ n");
    $text = str_replace($re move, '<table>', $text);

    see: http://www.php.net/str_replace

    Hope it helps,


    ..soma


    Comment

    • Cameron

      #3
      Re: Converting certain newlines to &lt;br /&gt;s

      somaBoy MX wrote:[color=blue]
      > "Christophe r Finke" <christopherfin ke@hotmail.com> wrote...
      >
      > : Can anyone suggest a method for achieving this? I've tried different
      > : mixtures of regular expressions, but my skills with those are just not up
      > to
      > : par.
      >
      > Christopher, first off it's never a good idea to allow people to enter html
      > code in forms of a content management service. You can allow them to insert
      > simple tags like <i>,<b> and <u> but if you're allowing them to create
      > things like tables it could seriously screw up the design of the site.
      >
      > A practical solution for your problem would be to do a str_replace on the
      > text before you store it. For example:
      >
      > $remove = array("<table>\ n", "<table>\r" , "<table>\r\ n");
      > $text = str_replace($re move, '<table>', $text);
      >
      > see: http://www.php.net/str_replace
      >
      > Hope it helps,
      >
      >
      > .soma
      >
      >[/color]

      Well, depending on how much work you want to do, which wouldn't be all
      that much really, you could let them put tables in providing that you
      check the syntax, running it through PHP's XML Parser actually may do
      the trick, in the effort to make sure for every opening tag we have a
      closing tag because tables are only really a pain in that scenario if
      they close and don't open or such like.


      ~Cameron

      Comment

      • Tim Van Wassenhove

        #4
        Re: Converting certain newlines to &lt;br /&gt;s

        On 2004-01-29, Christopher Finke <christopherfin ke@hotmail.com> wrote:[color=blue]
        > I have a site wherein the clients can update the individual pages by editing
        > the text of that page in a <textarea>. Then, when the page is displayed,
        > all of the newlines (\n) are converted to HTML line breaks (<br />).
        > However, I have come across some instances where I do not want to convert
        > the newlines, such as the following:
        >
        ><table>
        ><tr>
        > ...
        ></tr>
        ></table>
        >
        > Obviously, I would not want this to be
        >
        ><table><br />
        ><tr><br />
        > ...
        ></tr><br />
        ></table><br />[/color]

        I think that if you let them write <table> etc tags, they should be
        aware of the fact that newlines don't show up the way they expect it
        too.

        If you use output buffering, you could run tidy before outputting the
        lot.


        --

        Comment

        • Chung Leong

          #5
          Re: Converting certain newlines to &lt;br /&gt;s

          It can't be done. You either accept HTML as input or accept plain text. Your
          computer can't resolve the ambiguity when you mix the two up.

          Uzytkownik "Christophe r Finke" <christopherfin ke@hotmail.com> napisal w
          wiadomosci news:bv9jue$psf ic$1@ID-158177.news.uni-berlin.de...[color=blue]
          > I have a site wherein the clients can update the individual pages by[/color]
          editing[color=blue]
          > the text of that page in a <textarea>. Then, when the page is displayed,
          > all of the newlines (\n) are converted to HTML line breaks (<br />).
          > However, I have come across some instances where I do not want to convert
          > the newlines, such as the following:
          >
          > <table>
          > <tr>
          > ...
          > </tr>
          > </table>
          >
          > Obviously, I would not want this to be
          >
          > <table><br />
          > <tr><br />
          > ...
          > </tr><br />
          > </table><br />
          >
          > Can anyone suggest a method for achieving this? I've tried different
          > mixtures of regular expressions, but my skills with those are just not up[/color]
          to[color=blue]
          > par.
          >
          > Christopher Finke
          >
          >[/color]


          Comment

          Working...