By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Wrap() that doesn't split markup

P: n/a
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?
Oct 23 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Oct 23, 9:42*am, Gordon <gordon.mc...@ntlworld.comwrote:
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
Oct 24 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.