Created
April 27, 2014 14:00
-
-
Save jk/11346322 to your computer and use it in GitHub Desktop.
Smart Truncate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| header('Content-type: text/plain; charset=utf-8'); | |
| /** | |
| * Found on:http://stackoverflow.com/a/1193598/408150 | |
| */ | |
| function printTruncated($maxLength, $html, $isUtf8=true) | |
| { | |
| $printedLength = 0; | |
| $position = 0; | |
| $tags = array(); | |
| // For UTF-8, we need to count multibyte sequences as one character. | |
| $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)) | |
| { | |
| list($tag, $tagPosition) = $match[0]; | |
| // Print text leading up to the tag. | |
| $str = substr($html, $position, $tagPosition - $position); | |
| if ($printedLength + strlen($str) > $maxLength) | |
| { | |
| print(substr($str, 0, $maxLength - $printedLength)); | |
| $printedLength = $maxLength; | |
| break; | |
| } | |
| print($str); | |
| $printedLength += strlen($str); | |
| if ($printedLength >= $maxLength) break; | |
| if ($tag[0] == '&' || ord($tag) >= 0x80) | |
| { | |
| // Pass the entity or UTF-8 multibyte sequence through unchanged. | |
| print($tag); | |
| $printedLength++; | |
| } | |
| else | |
| { | |
| // Handle the tag. | |
| $tagName = $match[1][0]; | |
| if ($tag[1] == '/') | |
| { | |
| // This is a closing tag. | |
| $openingTag = array_pop($tags); | |
| assert($openingTag == $tagName); // check that tags are properly nested. | |
| print($tag); | |
| } | |
| else if ($tag[strlen($tag) - 2] == '/') | |
| { | |
| // Self-closing tag. | |
| print($tag); | |
| } | |
| else | |
| { | |
| // Opening tag. | |
| print($tag); | |
| $tags[] = $tagName; | |
| } | |
| } | |
| // Continue after the tag. | |
| $position = $tagPosition + strlen($tag); | |
| } | |
| // Print any remaining text. | |
| if ($printedLength < $maxLength && $position < strlen($html)) | |
| print(substr($html, $position, $maxLength - $printedLength)); | |
| // Close any open tags. | |
| while (!empty($tags)) | |
| printf('</%s>', array_pop($tags)); | |
| } | |
| printTruncated(10, '<b><Hello></b> <img src="world.png" alt="" /> world!'); print("\n"); | |
| printTruncated(10, '<table><tr><td>Heck, </td><td>throw</td></tr><tr><td>in a</td><td>table</td></tr></table>'); print("\n"); | |
| printTruncated(10, "<em><b>Hello</b>w\xC3\xB8rld!</em>"); print("\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment