3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php //+ Jonas Raoni Soares Silva //@ http://jsfromhell.com class String{ public static function truncate($s, $l, $e = '...', $isHTML = false){ $i = 0; $tags = array(); if($isHTML){ preg_match_all('/<[^>]+>([^<]*)/', $s, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); foreach($m as $o){ if($o[0][1] - $i >= $l) break; $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1); if($t[0] != '/') $tags[] = $t; elseif(end($tags) == substr($t, 1)) array_pop($tags); $i += $o[1][1] - $o[0][1]; } } return substr($s, 0, $l = min(strlen($s), $l + $i)) . (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '') . (strlen($s) > $l ? $e : ''); } } echo String::truncate('jo<i><b>n</b>as</i>', 3, '&hellip;'); //jo<... echo "\n\n\n"; echo String::truncate('jo<i><b>n</b>as</i>', 3, '&hellip;', true); //jo<i><b>n</b></i>... echo "\n\n\n"; echo String::truncate('jo<i><b>n</b>as</i>', 3, '&hellip;', true, false); //jo<i><b>n...
Output for 5.0.0 - 5.0.5, 5.1.1 - 5.6.28, hhvm-3.12.0
jo<&hellip; jo<i><b>n</b></i>&hellip; jo<i><b>n</b></i>&hellip;
Output for hhvm-3.10.0

Process exited with code 153.
Output for 5.1.0
Fatal error: fatal flex scanner internal error--end of buffer missed in /in/3LU41 on line 31
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/3LU41 on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/3LU41 on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/3LU41 on line 6
Process exited with code 255.