1. init an array to hold html tags (@html) 2. read $in (the htmlized text char by char) 3. $n++ for every non-html char (that is, !~ <.*> or <\/.*> 4. add the char (html or otherwise) to $out 5. push each html open tag (<.*>) in @html 6. on encountering a close tag (<\/.*>, 6.1. search @html for its corresponding open tag 6.2. and delete it from the array 7. stop when $n reaches the limit 8. add closing tags for all remaining open tags in @html in reverse order to the end of $out 9. spit $out