exeptions for nl2br --> preg_replace??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Snoek

    exeptions for nl2br --> preg_replace??

    Hi,

    Hope you can help me out with this one..

    nl2br($string) converts all the \n to <br \>

    ... but ..

    Suppose I have a table or other HTML elements in $string..
    All the TD> and TR> and FORM> are trailed by <br />

    How can I adjust the function:
    preg_replace("/\r\n|\r|\n/", "<br>\n", $r[body])

    to ignore lines ending with >
    (or: > n spaces )


    Thanx in advance,
    Marco Snoek



  • Andy Hassall

    #2
    Re: exeptions for nl2br --&gt; preg_replace??

    On Thu, 11 Mar 2004 20:21:33 +0100, "Marco Snoek"
    <_[mps]@[webmind.nl]_Dont_you_dare_ send.spam> wrote:
    [color=blue]
    >Hope you can help me out with this one..
    >
    >nl2br($strin g) converts all the \n to <br \>
    >
    >.. but ..
    >
    >Suppose I have a table or other HTML elements in $string..
    >All the TD> and TR> and FORM> are trailed by <br />
    >
    >How can I adjust the function:
    >preg_replace ("/\r\n|\r|\n/", "<br>\n", $r[body])
    >
    >to ignore lines ending with >
    >(or: > n spaces )[/color]

    I think you're heading into trouble with this approach; what about the
    following perfectly valid HTML:

    <form method="get"
    action="somethi ng.php"
    name="whatever" >

    You've now got a whole load of other exceptions to deal with.

    The idea of nl2br is to convert _non-HTML_ text data containing newlines to an
    HTML equivalent that has <br> elements so that it has newlines in the same
    place when inserted into an HTML document.

    In general, you can't parse HTML with a single regular expression, as regular
    expressions aren't capable of maintaining states as necessary to implement a
    HTML parser.

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

    Comment

    • Marco Snoek

      #3
      Re: exeptions for nl2br --&gt; preg_replace??

      You're absolutely right..
      But....
      I'm quite sure the trick should work.. I enter the input myself :-)

      Can you think of a reg_exp ???

      Regards...

      Marco


      "Andy Hassall" <andy@andyh.co. uk> schreef in bericht
      news:8jo1509m0t nchr6pv6qe325sq cb4goi80k@4ax.c om...[color=blue]
      > On Thu, 11 Mar 2004 20:21:33 +0100, "Marco Snoek"
      > <_[mps]@[webmind.nl]_Dont_you_dare_ send.spam> wrote:
      >[color=green]
      > >Hope you can help me out with this one..
      > >
      > >nl2br($strin g) converts all the \n to <br \>
      > >
      > >.. but ..
      > >
      > >Suppose I have a table or other HTML elements in $string..
      > >All the TD> and TR> and FORM> are trailed by <br />
      > >
      > >How can I adjust the function:
      > >preg_replace ("/\r\n|\r|\n/", "<br>\n", $r[body])
      > >
      > >to ignore lines ending with >
      > >(or: > n spaces )[/color]
      >
      > I think you're heading into trouble with this approach; what about the
      > following perfectly valid HTML:
      >
      > <form method="get"
      > action="somethi ng.php"
      > name="whatever" >
      >
      > You've now got a whole load of other exceptions to deal with.
      >
      > The idea of nl2br is to convert _non-HTML_ text data containing newlines[/color]
      to an[color=blue]
      > HTML equivalent that has <br> elements so that it has newlines in the same
      > place when inserted into an HTML document.
      >
      > In general, you can't parse HTML with a single regular expression, as[/color]
      regular[color=blue]
      > expressions aren't capable of maintaining states as necessary to implement[/color]
      a[color=blue]
      > HTML parser.
      >
      > --
      > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>[/color]


      Comment

      • Pedro Graca

        #4
        Re: exeptions for nl2br --&gt; preg_replace??

        Marco Snoek wrote:[color=blue]
        > Can you think of a reg_exp ???[/color]

        You need the "lookbehind " stuff.

        Open this URLi: http://www.php.net/manual/en/pcre.pattern.syntax.php
        and search "lookbehind " in that page
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        Working...