Created
October 14, 2011 18:14
-
-
Save thurloat/1287868 to your computer and use it in GitHub Desktop.
Word Lopper
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 | |
| /** | |
| * Ensures that line lengths fit within a chunk size, cares for HTML, HTML entities, and words. | |
| */ | |
| $scanning = true; | |
| $inHtml = false; | |
| $pieces = array(); | |
| $cursor = 0; | |
| $chunkSize = 30; | |
| $os = ""; | |
| $a = "asdf<p awesome='&true;'>these are some real words woot </p>"; | |
| print "Input: \n" . $a . "\n\n"; | |
| print "Lopping off at " . $chunkSize; | |
| while ($scanning) { | |
| print "\n\n Scanning Again, num pieces: " . count($pieces); | |
| if (strlen($a) < $cursor + $chunkSize){ | |
| print strlen($a) . " is less than " . ($cursor + $chunkSize); | |
| print "\n Reached end of string"; | |
| $scanning = false; | |
| // $chunkSize = strlen($a) - $cursor + 2; | |
| print "\n New chunk size of " . $chunkSize; | |
| } | |
| $ws = substr($a, $cursor, $chunkSize); | |
| $inHtml = false; | |
| print "\n\t Scanning through string '". $ws."'"; | |
| for ($i = 0; $i < strlen($ws); $i++){ | |
| $s = $ws[$i]; | |
| if ($s == "<"){ | |
| $inHtml = true; | |
| $badChar = $i; | |
| print "\n\t Found HTML at " . $i; | |
| } elseif ($s == ">"){ | |
| $inHtml = false; | |
| print "\n\t Found End of HTML at " . $i; | |
| } | |
| if (!$inHtml) { | |
| // not in HTML, check for entities | |
| if ($s == "&"){ | |
| $inBad = true; | |
| $badChar = $i; | |
| print "\n\t Found HTML Entity at " . $i; | |
| } elseif ($s == ";" && $inBad){ | |
| $inBad = false; | |
| print "\n\t Found End of HTML Entity at " . $i; | |
| } | |
| if (!$inBad){ | |
| // no html, no entities, check that words don't get cut off as well. | |
| if ($s != " ") { | |
| $inBad = true; | |
| $badChar = $i; | |
| } else { | |
| $inBad = false; | |
| } | |
| } | |
| if ($s == " " || $s == ">" || $s == ";"){ | |
| $inBad = false; | |
| } | |
| } | |
| } | |
| if ($inHtml || $inBad){ | |
| print "\n\t Trimming back to position " . $badChar . " no end was found within the boundary"; | |
| if ($ws == $os) { | |
| // unable to safely break the words. | |
| print "\n\n Unable to safely break words. Doing it anyway."; | |
| array_push($pieces, substr($a, $cursor, $chunkSize)); | |
| $cursor += $chunkSize; | |
| } else { | |
| array_push($pieces, substr($a, $cursor, $badChar)); | |
| $cursor += $badChar; | |
| } | |
| } else { | |
| print "\n\t String '". $ws ."' looks safe."; | |
| array_push($pieces, substr($a, $cursor, $chunkSize)); | |
| $cursor += $chunkSize; | |
| } | |
| $os = $ws; | |
| } | |
| print "Output \n\n"; | |
| print implode("\r\n", $pieces); |
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
| Input: | |
| asdf<p awesome='&true;'>these are some real words woot </p> | |
| Lopping off at 30 | |
| Scanning Again, num pieces: 0 | |
| Scanning through string 'asdf<p awesome='&true;'>these ' | |
| Found HTML at 4 | |
| Found End of HTML at 23 | |
| String 'asdf<p awesome='&true;'>these ' looks safe. | |
| Scanning Again, num pieces: 1 | |
| Scanning through string 'are some real words woo' | |
| Found HTML Entity at 20 | |
| Found End of HTML Entity at 25 | |
| Trimming back to position 27 no end was found within the boundary | |
| Scanning Again, num pieces: 266 is less than 87 | |
| Reached end of string | |
| New chunk size of 30 | |
| Scanning through string 'woot </p>' | |
| Found HTML at 5 | |
| Found End of HTML at 8 | |
| String 'woot </p>' looks safe.Output | |
| asdf<p awesome='&true;'>these | |
| are some real words | |
| woot </p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment