Tuesday, 20 August 2013

preg_split but ignore XML and HTML entities

preg_split but ignore XML and HTML entities

I using this php code to split a string roughly every 120 chars. IT splits
at the closest space. But it splits HTML and XML entities, so it sometimes
outputs things like: id="id">. how can i make it so it ignores xml and
html entities, but does not remove them.
function splitWords($string, $max = 1)
{
$words = preg_split( '/\s/', $string );
$lines = array();
$line = '';
foreach ( $words as $k => $word ) {
$newLine = $line . ' ' . $word;
$length = strlen( $newLine );
if ( $length <= $max ) {
$line .= ' ' . $word;
} else if ( $length > $max ) {
if ( !empty( $line ) ) {
$lines[] = trim( $line );
}
$line = $word;
} else {
$lines[] = trim( $line ) . ' ' . $word;
$line = '';
}
}
$lines[] = ( $line = trim( $line ) ) ? $line : $word;
return $lines;
}
thanks
Rob

No comments:

Post a Comment