3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function str_truncat($string, $nbrchar = 50, $charsfin = "", $addstrfin = "...") { if($charsfin == "" && strlen(strip_tags($string)) < $nbrchar) { $stringtruncat = strip_tags($string); } else { $nbr = ($nbrchar)/250; $addchars = ""; if($nbr > 1) { for($i = 0;$i < (int)$nbr;$i++) { $nbrchar -= 250; $addchars .= ".{250}"; } } $addchars .= ".{".$nbrchar."}"; $str1 = preg_replace("(\[+[a-z0-9]+\]{1,2})", "", str_replace("\n"," ",strip_tags($string))); $str2 = preg_replace("(\[+[a-zA-Z:0-9]+[ |]([^\]]+)*\]+)", "\\1 ", $str1); $preg = "/^(".$addchars.($charsfin?"[^".$charsfin."]*":"").").*$/"; $str3 = preg_replace($preg, "\\1".$addstrfin, $str2); $stringtruncat = preg_replace("/ ".str_replace(".", "\.", $addstrfin)."$/", $addstrfin, $str3); } return htmlspecialchars($stringtruncat, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'); } $var = "LG 47LA640S 859 € dont 8 &euro; d'écocontribution"; $var = str_truncat($var); $var = html_entity_decode($var, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'); echo $var;
Output for 5.4.0 - 7.1.0
LG 47LA640S 859 € dont 8 &euro; d'écocontributi...
Output for 4.3.2 - 5.3.29
Notice: Use of undefined constant ENT_HTML401 - assumed 'ENT_HTML401' in /in/EvHKs on line 24 Notice: Use of undefined constant ENT_HTML401 - assumed 'ENT_HTML401' in /in/EvHKs on line 29 LG 47LA640S 859 € dont 8 &euro; d'écocontributi...
Output for 4.3.0 - 4.3.1
Notice: Use of undefined constant ENT_HTML401 - assumed 'ENT_HTML401' in /in/EvHKs on line 24
Process exited with code 139.