Wrap() that doesn't split markup

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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?

Re: Wrap() that doesn't split markup

Quoted text here. Click to load it

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    =3D wordwrap ($string, $width, $break);
    return (preg_replace ('/(<[^>]*)\n([^<]*>)/is', ' ', $string));

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


This is some <a href=3D"#"
target=3D"_self">link</a> to some page somewhere


This is some <a href=3D"#"
 target=3D"_self">link</a> to some page somewhere

Site Timeline