grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Library;
defined('_JEXEC') or die;
use DOMDocument;
/**
* Class Html
* @package RegularLabs\Library
*/
class Html
{
/**
* Convert content saved in a WYSIWYG editor to plain text (like removing html tags)
*
* @param $string
*
* @return string
*/
public static function convertWysiwygToPlainText($string)
{
// replace chr style enters with normal enters
$string = str_replace([chr(194) . chr(160), ' ', ' '], ' ', $string);
// replace linebreak tags with normal linebreaks (paragraphs, enters, etc).
$enter_tags = ['p', 'br'];
$regex = '?((' . implode(')|(', $enter_tags) . '))+[^>]*?>\n?';
$string = RegEx::replace($regex, " \n", $string);
// replace indent characters with spaces
$string = RegEx::replace(']*/sourcerer/images/tab\.png[^>]*>', ' ', $string);
// strip all other tags
$regex = '<(/?\w+((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)/?)>';
$string = RegEx::replace($regex, '', $string);
// reset htmlentities
$string = StringHelper::html_entity_decoder($string);
// convert protected html entities &_...; -> &...;
$string = RegEx::replace('&_([a-z0-9\#]+?);', '&\1;', $string);
return $string;
}
/**
* Extract the
]*)?>\s*
|]*)?>\s*
\s*$)', '', $body); return $pre . $body . $post; } /** * Fix broken/invalid html syntax in a string using custom code as an alternative to php DOMDocument functionality * * @param string $string * * @return string */ private static function fixUsingCustomFixer($string) { $block_regex = '<(' . implode('|', self::getBlockElementsNoDiv()) . ')[\s>]'; $string = RegEx::replace('(' . $block_regex . ')', '[:SPLIT-BLOCK:]\1', $string); $parts = explode('[:SPLIT-BLOCK:]', $string); foreach ($parts as $i => &$part) { if ( ! RegEx::match('^' . $block_regex, $part, $type)) { continue; } $type = strtolower($type[1]); // remove endings of other block elements $part = RegEx::replace('(?:' . implode('|', self::getBlockElementsNoDiv($type)) . ')>', '', $part); if (strpos($part, '' . $type . '>') !== false) { continue; } // Add ending tag once $part = RegEx::replaceOnce('(\s*)$', '' . $type . '>\1', $part); // Remove empty block tags $part = RegEx::replace('^<' . $type . '(?: [^>]*)?>\s*' . $type . '>', '', $part); } return implode('', $parts); } /** * Removes complete html tag pairs from the concatenated parts * * @param array $parts * @param array $elements * * @return array */ public static function cleanSurroundingTags($parts, $elements = ['p', 'span']) { $breaks = '(?:(?:]*)?>)(\s*' . $breaks . ')(<' . $block_elements . '(?: [^>]*)?>)'; while (RegEx::match($regex, $string, $match)) { if ($match[4] == 'p') { $match[3] = $match[1] . $match[3]; self::combinePTags($match[3]); } $string = str_replace($match[0], $match[2] . $match[3], $string); } $regex = '(' . $block_elements . '>\s*' . $breaks . ')
'; while (RegEx::match($regex, $string, $match)) { $string = str_replace($match[0], $match[1], $string); } $parts = explode(':|:', $string); $new_tags = []; foreach ($parts as $key => $val) { $key = isset($keys[$key]) ? $keys[$key] : $key; $new_tags[$key] = $val; } return $new_tags; } /** * Removetags around block elements * * @param string $string * * @return mixed */ private static function removeParagraphsAroundBlockElements($string) { if (strpos($string, '
') == false) { return $string; } $string = RegEx::replace( ']*)?>\s*' . '((?:<\!--.*?-->\s*)*?(?:' . implode('|', self::getBlockElements()) . ')' . '(?: [^>]*)?>)', '\1', $string ); $string = RegEx::replace( '(?(?:' . implode('|', self::getBlockElements()) . ')' . '(?: [^>]*)?>(?:\s*<\!--.*?-->)*)' . '(?:\s*
)', '\1', $string ); return $string; } /** * Removetags around comments * * @param string $string * * @return mixed */ private static function removeParagraphsAroundComments($string) { if (strpos($string, '
') == false) { return $string; } $string = RegEx::replace( '(?:]*)?>\s*)' . '(<\!--.*?-->)' . '(?:\s*
)', '\1', $string ); return $string; } /** * Fixtags around other
elements * * @param string $string * * @return mixed */ private static function fixParagraphsAroundParagraphElements($string) { if (strpos($string, '
') == false) { return $string; } $parts = explode('', $string); $ending = '' . array_pop($parts); foreach ($parts as &$part) { if (strpos($part, '') === false && strpos($part, '
' . $part; continue; } $part = RegEx::replace( '(
]*)?>.*?)(
]*)?>)', '\1
\2', $part ); } return implode('', $parts) . $ending; } /* * Remove empty tags * * @param string $string * @param array $elements * * @return mixed */ public static function removeEmptyTagPairs($string, $elements = ['p', 'span']) { $breaks = '(?:(?:tags * input:
* output:
* * @param $string */ public static function combinePTags(&$string) { if (empty($string)) { return; } $p_start_tag = '
]*)?>'; $optional_tags = '\s*(?:<\!--.*?-->| |&\#160;)*\s*'; RegEx::matchAll('(' . $p_start_tag . ')(' . $optional_tags . ')(' . $p_start_tag . ')', $string, $tags); if (empty($tags)) { return; } foreach ($tags as $tag) { $string = str_replace($tag[0], $tag[2] . HtmlTag::combine($tag[1], $tag[3]), $string); } } /** * Remove inline elements around block elements * * @param string $string * * @return mixed */ public static function removeInlineElementsAroundBlockElements($string) { $string = RegEx::replace( '(?:<(?:' . implode('|', self::getInlineElementsNoAnchor()) . ')(?: [^>]*)?>\s*)' . '(?(?:' . implode('|', self::getBlockElements()) . ')(?: [^>]*)?>)', '\1', $string ); $string = RegEx::replace( '(?(?:' . implode('|', self::getBlockElements()) . ')(?: [^>]*)?>)' . '(?:\s*(?:' . implode('|', self::getInlineElementsNoAnchor()) . ')>)', '\1', $string ); return $string; } /** * Return an array of block element names, optionally without any of the names given $exclude * * @param array $exclude * * @return array */ public static function getBlockElements($exclude = []) { if ( ! is_array($exclude)) { $exclude = [$exclude]; } $elements = [ 'div', 'p', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', ]; $elements = array_diff($elements, $exclude); $elements = implode(',', $elements); $elements = str_replace('h1,h2,h3,h4,h5,h6', 'h[1-6]', $elements); $elements = explode(',', $elements); return $elements; } /** * Return an array of inline element names, optionally without any of the names given $exclude * * @param array $exclude * * @return array */ public static function getInlineElements($exclude = []) { if ( ! is_array($exclude)) { $exclude = [$exclude]; } $elements = [ 'span', 'code', 'a', 'strong', 'b', 'em', 'i', 'u', 'big', 'small', 'font', 'sup', 'sub', ]; return array_diff($elements, $exclude); } /** * Return an array of block element names, without divs and any of the names given $exclude * * @param array $exclude * * @return array */ public static function getBlockElementsNoDiv($exclude = []) { return array_diff(self::getBlockElements($exclude), ['div']); } /** * Return an array of block element names, without anchors (a) and any of the names given $exclude * * @param array $exclude * * @return array */ public static function getInlineElementsNoAnchor($exclude = []) { return array_diff(self::getInlineElements($exclude), ['a']); } /** * Protect plugin style tags and php * * @param $string * * @return mixed */ private static function protectSpecialCode($string) { // Protect PHP code Protect::protectByRegex($string, '(<|<)\?php\s.*?\?(>|>)'); // Protect {...} tags Protect::protectByRegex($string, '\{[a-z0-9].*?\}'); // Protect [...] tags Protect::protectByRegex($string, '\[[a-z0-9].*?\]'); // Protect scripts Protect::protectByRegex($string, ''); // Protect css Protect::protectByRegex($string, ''); Protect::convertProtectionToHtmlSafe($string); return $string; } /** * Unprotect protected tags * * @param $string * * @return mixed */ private static function unprotectSpecialCode($string) { Protect::unprotectHtmlSafe($string); return $string; } /** * Prevents broken html tags at the end of $pre (other half at beginning of $string) * It will move the broken part to the beginning of $string to complete it * * @param $pre * @param $string */ private static function fixBrokenTagsByPreString(&$pre, &$string) { if ( ! RegEx::match('<(\![^>]*|/?[a-z][^>]*(="[^"]*)?)$', $pre, $match)) { return; } $pre = substr($pre, 0, strlen($pre) - strlen($match[0])); $string = $match[0] . $string; } /** * Prevents broken html tags at the beginning of $pre (other half at end of $string) * It will move the broken part to the end of $string to complete it * * @param $post * @param $string */ private static function fixBrokenTagsByPostString(&$post, &$string) { if ( ! RegEx::match('<(\![^>]*|/?[a-z][^>]*(="[^"]*)?)$', $string, $match)) { return; } if ( ! RegEx::match('^[^>]*>', $post, $match)) { return; } $post = substr($post, strlen($match[0])); $string .= $match[0]; } /** * Removes html tags from string * * @param string $string * * @return string */ public static function removeHtmlTags($string) { // remove pagenavcounter $string = RegEx::replace('