+
+ # Now let's actually convert our apostrophic mush to HTML!
+ $output = '';
+ $buffer = '';
+ $state = '';
+ $i = 0;
+ foreach ($arr as $r)
+ {
+ if (($i % 2) == 0)
+ {
+ if ($state == 'both')
+ $buffer .= $r;
+ else
+ $output .= $r;
+ }
+ else
+ {
+ if (strlen ($r) == 2)
+ {
+ if ($state == 'em')
+ { $output .= "</em>"; $state = ''; }
+ else if ($state == 'strongem')
+ { $output .= "</em>"; $state = 'strong'; }
+ else if ($state == 'emstrong')
+ { $output .= "</strong></em><strong>"; $state = 'strong'; }
+ else if ($state == 'both')
+ { $output .= "<strong><em>{$buffer}</em>"; $state = 'strong'; }
+ else # $state can be 'strong' or ''
+ { $output .= "<em>"; $state .= 'em'; }
+ }
+ else if (strlen ($r) == 3)
+ {
+ if ($state == 'strong')
+ { $output .= "</strong>"; $state = ''; }
+ else if ($state == 'strongem')
+ { $output .= "</em></strong><em>"; $state = 'em'; }
+ else if ($state == 'emstrong')
+ { $output .= "</strong>"; $state = 'em'; }
+ else if ($state == 'both')
+ { $output .= "<em><strong>{$buffer}</strong>"; $state = 'em'; }
+ else # $state can be 'em' or ''
+ { $output .= "<strong>"; $state .= 'strong'; }
+ }
+ else if (strlen ($r) == 5)
+ {
+ if ($state == 'strong')
+ { $output .= "</strong><em>"; $state = 'em'; }
+ else if ($state == 'em')
+ { $output .= "</em><strong>"; $state = 'strong'; }
+ else if ($state == 'strongem')
+ { $output .= "</em></strong>"; $state = ''; }
+ else if ($state == 'emstrong')
+ { $output .= "</strong></em>"; $state = ''; }
+ else if ($state == 'both')
+ { $output .= "<em><strong>{$buffer}</strong></em>"; $state = ''; }
+ else # ($state == '')
+ { $buffer = ''; $state = 'both'; }
+ }
+ }
+ $i++;
+ }
+ # Now close all remaining tags. Notice that the order is important.
+ if ($state == 'strong' || $state == 'emstrong')
+ $output .= "</strong>";
+ if ($state == 'em' || $state == 'strongem' || $state == 'emstrong')
+ $output .= "</em>";
+ if ($state == 'strongem')
+ $output .= "</strong>";
+ if ($state == 'both')
+ $output .= "<strong><em>{$buffer}</em></strong>";
+ return $output;