$/s', $html, $matches)) { $tag = strtolower($matches[1]); $attrsContent = $matches[2]; // Parse attributes preg_match_all('/([a-zA-Z0-9:-]+)=["\'](.*?)["\']/s', $attrsContent, $attrMatches, PREG_SET_ORDER); $attrs = []; foreach ($attrMatches as $match) { $attrs[strtolower($match[1])] = trim($match[2]); } ksort($attrs); $attrStr = ''; foreach ($attrs as $name => $value) { $attrStr .= sprintf(' %s="%s"', $name, $value); } return sprintf('<%s%s>', $tag, $attrStr); } // Fallback: Strip all whitespace and lowercase $cleaned = preg_replace('/\s+/', '', $html) ?: ''; return strtolower($cleaned); } }