1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-28 10:20:45 +02:00

Bugtracker #3972 - strip empty HTML tags in wordwrap

This commit is contained in:
e107steved
2007-12-30 09:49:46 +00:00
parent ac7f0ddf36
commit 47c6faff35

View File

@@ -11,8 +11,8 @@
| GNU General Public License (http://gnu.org). | GNU General Public License (http://gnu.org).
| |
| $Source: /cvs_backup/e107_0.8/e107_handlers/e_parse_class.php,v $ | $Source: /cvs_backup/e107_0.8/e107_handlers/e_parse_class.php,v $
| $Revision: 1.23 $ | $Revision: 1.24 $
| $Date: 2007-12-07 21:24:55 $ | $Date: 2007-12-30 09:49:46 $
| $Author: e107steved $ | $Author: e107steved $
+----------------------------------------------------------------------------+ +----------------------------------------------------------------------------+
*/ */
@@ -204,73 +204,125 @@ class e_parse
} }
function htmlwrap($str, $width, $break = "\n", $nobreak = "", $nobr = "pre", $utf = false) function htmlwrap($str, $width, $break = "\n", $nobreak = "", $nobr = "pre", $utf = false)
{ {
/* /*
* htmlwrap() function - v1.1 * Parts of code from htmlwrap() function - v1.6
* Copyright (c) 2004 Brian Huisman AKA GreyWyvern * Copyright (c) 2004 Brian Huisman AKA GreyWyvern
* http://www.greywyvern.com/code/php/htmlwrap_1.1.php.txt * http://www.greywyvern.com/code/php/htmlwrap_1.1.php.txt
* *
* This program may be distributed under the terms of the GPL * This program may be distributed under the terms of the GPL
* - http://www.gnu.org/licenses/gpl.txt * - http://www.gnu.org/licenses/gpl.txt
*
* Other mods by steved
*/ */
$content = preg_split("/([<>])/", $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); // Transform protected element lists into arrays
$nobreak = explode(" ", $nobreak); $nobreak = explode(" ", strtolower($nobreak));
$nobr = explode(" ", $nobr);
$intag = false; // Variable setup
$innbk = array(); $intag = false;
$innbr = array(); $innbk = array();
$drain = ""; $drain = "";
$utf = ($utf || CHARSET == 'utf-8') ? "u" : "";
$lbrks = "/?!%)-}]\\\"':;"; // List of characters it is "safe" to insert line-breaks at
if ($break == "\r") // It is not necessary to add < and > as they are automatically implied
$lbrks = "/?!%)-}]\\\"':;&";
// Is $str a UTF8 string?
$utf8 = ($utf || CHARSET == 'utf-8') ? "u" : "";
// Start of the serious stuff - split into HTML tags and text between
$content = preg_split('#(<.*?>)#mis', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
foreach($content as $value)
{
if ($value[0] == "<")
{ // We are within an HTML tag
// Create a lowercase copy of this tag's contents
$lvalue = strtolower(substr($value,1,-1));
if ($lvalue)
{ // Tag of non-zero length
// If the first character is not a / then this is an opening tag
if ($lvalue[0] != "/")
{ // Collect the tag name
preg_match("/^(\w*?)(\s|$)/", $lvalue, $t);
// If this is a protected element, activate the associated protection flag
if (in_array($t[1], $nobreak)) array_unshift($innbk, $t[1]);
}
else
{ // Otherwise this is a closing tag
// If this is a closing tag for a protected element, unset the flag
if (in_array(substr($lvalue, 1), $nobreak))
{
reset($innbk);
while (list($key, $tag) = each($innbk))
{
if (substr($lvalue, 1) == $tag)
{
unset($innbk[$key]);
break;
}
}
$innbk = array_values($innbk);
}
}
}
else
{
$value = ''; // Eliminate any empty tags altogether
}
// Else if we're outside any tags, and with non-zero length string...
}
elseif ($value)
{ {
$break = "\n"; // If unprotected...
} if (!count($innbk))
while (list(, $value) = each($content)) {
{ // Use the ACK (006) ASCII symbol to replace all HTML entities temporarily
switch ($value) $value = str_replace("\x06", "", $value);
preg_match_all("/&([a-z\d]{2,7}|#\d{2,5});/i", $value, $ents);
$value = preg_replace("/&([a-z\d]{2,7}|#\d{2,5});/i", "\x06", $value);
// Enter the line-break loop
do
{ {
case "<": $intag = true; break; $store = $value;
case ">": $intag = false; break;
default: // Find the first stretch of characters over the $width limit
if ($intag) if (preg_match("/^(.*?\s)?(\S{".$width."})(?!(".preg_quote($break, "/")."|\s))(.*)$/s".(($utf8) ? "u" : ""), $value, $match))
{
if (strlen($match[2]))
{ {
if ($value{0} != "/") // Determine the last "safe line-break" character within this match
{ for ($x = 0, $ledge = 0; $x < strlen($lbrks); $x++) $ledge = max($ledge, strrpos($match[2], $lbrks{$x}));
preg_match('/^(.*?)(\s|$)/'.$utf, $value, $t); if (!$ledge) $ledge = strlen($match[2]) - 1;
if ((!count($innbk) && in_array($t[1], $nobreak)) || in_array($t[1], $innbk)) $innbk[] = $t[1];
if ((!count($innbr) && in_array($t[1], $nobr)) || in_array($t[1], $innbr)) $innbr[] = $t[1]; // Insert the modified string
} else { $value = $match[1].substr($match[2], 0, $ledge + 1).$break.substr($match[2], $ledge + 1).$match[4];
if (in_array(substr($value, 1), $innbk)) unset($innbk[count($innbk)]); }
if (in_array(substr($value, 1), $innbr)) unset($innbr[count($innbr)]); }
} // Loop while overlimit strings are still being found
} else if ($value) } while ($store != $value);
{
if (!count($innbr)) $value = str_replace("\n", "\r", str_replace("\r", "", $value)); // Put captured HTML entities back into the string
if (!count($innbk)) foreach ($ents[0] as $ent) $value = preg_replace("/\x06/", $ent, $value, 1);
{ }
do }
{
$store = $value; // Send the modified segment down the drain
if (preg_match("/^(.*?\s|^)(([^\s&]|&(\w{2,5}|#\d{2,4});){".$width."})(?!(".preg_quote($break, "/").'|\s))(.*)$/s'.$utf, $value, $match)) $drain .= $value;
{ }
for ($x = 0, $ledge = 0; $x < strlen($lbrks); $x++) $ledge = max($ledge, strrpos($match[2], $lbrks{$x}));
if (!$ledge) $ledge = strlen($match[2]) - 1; // Return contents of the drain
$value = $match[1].substr($match[2], 0, $ledge + 1).$break.substr($match[2], $ledge + 1).$match[6]; return $drain;
}
}
while ($store != $value);
}
if (!count($innbr)) $value = str_replace("\r", E_NL, $value);
}
}
$drain .= $value;
}
return $drain;
} }
function html_truncate ($text, $len = 200, $more = "[more]") function html_truncate ($text, $len = 200, $more = "[more]")
{ {
$pos = 0; $pos = 0;