Wrap() that doesn't split markup

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

    Wrap() that doesn't split markup

    I've been looking into including a system to allow logging of diffs so
    users can view different versions of a file and roll it back to an
    earlier version. After a lot of faffing with PHP diff functions and
    the like I'm thinking that the best way to do it is probably just to
    pass the data to the UNIX diff() program to get the diffs for the edit
    and use patch() for the rolling back.

    Unfortunately there's nothing that stops users entering just a single
    long line of markup, and as diff appears to be line-based this means
    the generated patches would be very space inefficient. I thought of
    running the user input through wordwrap() before committing it but
    that's problematic because it can split a HTML tag across lines.

    What I need is at least a wordwrap() that takes HTML markup into
    account, and I think that maybe some kind of HTML pretty printer would
    be better.

    Can anyone help with this?
  • Gordon

    #2
    Re: Wrap() that doesn't split markup

    On Oct 23, 9:42 am, Gordon <gordon.mc...@n tlworld.comwrot e:
    I've been looking into including a system to allow logging of diffs so
    users can view different versions of a file and roll it back to an
    earlier version.  After a lot of faffing with PHP diff functions and
    the like I'm thinking that the best way to do it is probably just to
    pass the data to the UNIX diff() program to get the diffs for the edit
    and use patch() for the rolling back.
    >
    Unfortunately there's nothing that stops users entering just a single
    long line of markup, and as diff appears to be line-based this means
    the generated patches would be very space inefficient.  I thought of
    running the user input through wordwrap() before committing it but
    that's problematic because it can split a HTML tag across lines.
    >
    What I need is at least a wordwrap() that takes HTML markup into
    account, and I think that maybe some kind of HTML pretty printer would
    be better.
    >
    Can anyone help with this?
    I had the idea of writing a function that does the following:

    1) wordwrap () the passed string
    2) find any HTML tags in the result that have a line break in them
    3) replace the line breaks with spaces

    I tried the following:

    function htmlWrap ($string, $width = 75, $break = "\n")
    {
    $string = wordwrap ($string, $width, $break);
    return (preg_replace ('/(<[^>]*)\n([^<]*>)/is', '\1 \2', $string));
    }

    This doesn't work, it only finds one of the line breaks and then
    replaces it with a break and a space.

    Input:

    This is some <a href="#"
    title="link"
    target="_self"> link</ato some page somewhere

    Output:

    This is some <a href="#"
    title="link"
    target="_self"> link</ato some page somewhere

    Comment

    Working...