忽略標籤時截斷包含HTML 的文字
嘗試截斷包含HTML 的文字時,通常會遇到標籤未正確關閉的問題,導致截斷結果失真。為了克服這個問題,有必要有效地解析 HTML 並處理標籤。
這是一種基於 PHP 的方法,可確保標籤在截斷期間正確關閉:
function printTruncated($maxLength, $html, $isUtf8=true) { $printedLength = 0; $position = 0; $tags = array(); // Regex pattern for matching HTML tags, entities, and UTF-8 characters $re = $isUtf8 ? '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;|[\x80-\xFF][\x80-\xBF]*}' : '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}'; while ($printedLength < $maxLength && preg_match($re, $html, $match, PREG_OFFSET_CAPTURE, $position)) { // 1. Handle text leading up to the tag $str = substr($html, $position, $match[0][1] - $position); if ($printedLength + strlen($str) <= $maxLength) { print($str); $printedLength += strlen($str); } else { print(substr($str, 0, $maxLength - $printedLength)); $printedLength = $maxLength; break; } // 2. Handle the tag $tag = $match[0][0]; if ($tag[0] == '&' || ord($tag) >= 0x80) { // Pass the entity or UTF-8 character through unchanged print($tag); $printedLength++; } else { $tagName = $match[1][0]; if ($tag[1] == '/') { // Closing tag $openingTag = array_pop($tags); assert($openingTag == $tagName); // Ensure proper tag nesting print($tag); } else if ($tag[strlen($tag) - 2] == '/') { // Self-closing tag print($tag); } else { // Opening tag print($tag); $tags[] = $tagName; } } $position = $match[0][1] + strlen($tag); } // 3. Print remaining text if ($printedLength < $maxLength && $position < strlen($html)) print(substr($html, $position, $maxLength - $printedLength)); // 4. Close any open tags while (!empty($tags)) printf('</%s>', array_pop($tags)); }
為了說明其功能:
printTruncated(10, '<b><Hello></b> <img src="world.png" alt="" /> world!'); // Output: <b><Hello></b> <img printTruncated(10, '<table><tr><td>Heck, </td><td>throw</td></tr><tr><td>in a</td><td>table</td></tr></table>'); // Output: <table><tr><td>Heck printTruncated(10, "<em><b>Hello</b>&#20;w\xC3\xB8rld!</em>"); // Output: <em><b>Hello</b> w
以上是如何截斷包含 HTML 的文本,同時確保正確的標籤閉合?的詳細內容。更多資訊請關注PHP中文網其他相關文章!