3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncate($text, $length = 100, $ending = ‘…’, $exact = true, $considerHtml = false) { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if( strlen(preg_replace('/<.*?>/', ”, $text)) <= $length) { return $text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); $open_tags = array(); $truncate = ”; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it’s an “empty element” with or without xhtml-conform closing slash (f.e. ) if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { // do nothing // if tag is a closing tag (f.e. ) } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { // delete tag from $open_tags list $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } // if tag is an opening tag (f.e. ) } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { // add tag to the beginning of $open_tags list array_unshift($open_tags, strtolower($tag_matchings[1])); } // add html-tag to $truncate’d text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ‘ ‘, $line_matchings[2])); if ($total_length+$content_length > $length) { // the number of characters which are left $left = $length – $total_length; $entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { if ($entity[1]+1-$entities_length <= $left) { $left--; $entities_length += strlen($entity[0]); } else { // no more characters left break; } } } $truncate .= substr($line_matchings[2], 0, $left+$entities_length); // maximum lenght is reached, so get off the loop break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } // if the maximum length is reached, get off the loop if($total_length >= $length) { break; } } } else { if (strlen($text) <= $length) { return $text; } else { $truncate = substr($text, 0, $length - strlen($ending)); } } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { // ...and cut the text in this position $truncate = substr($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; if($considerHtml) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= ''; } } return $truncate; } print truncate( '<p>Ключевым элементом в области снижения влияния объектов производства на водные ресурсы является уменьшение забора воды и качество <i>очистительных</i> процедур.</p> <p>Для <strong>этих</strong> целей Группа «Газпром» проводит организационно-технические мероприятия, <img src="http://osb.loc/images/logo.jpg" alt="test" text> в частности по установке средств измерения расхода воды, а также установке и наладке запорно-затворной арматуры на своих предприятиях, что способствует существенной экономии водных ресурсов.</p> <p>Повышение эффективности действующих очистных сооружений и внедрение новых технологий очистки стоков позволяет обеспечить нормативное качество очистки сточных вод. Общая мощность очистных сооружений «Газпрома» в 2010 г. составила 153,263 млн куб. м. Половина нормативно очищенных стоков прошла очистку на сооружениях биологической очистки, 39% — механической очистки, 12% — физико-химической очистки сточных вод.</p>', 30 );

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
5.4.300.0040.03812.53
5.4.290.0050.03912.52
5.4.280.0050.03712.42
5.4.270.0050.03812.42
5.4.260.0050.03912.42
5.4.250.0070.03612.41
5.4.240.0050.03712.42
5.4.230.0060.03812.41
5.4.220.0100.03212.41
5.4.210.0070.03412.41
5.4.200.0040.03812.41
5.4.190.0070.03412.41
5.4.180.0080.03412.41
5.4.170.0080.03512.41
5.4.160.0090.03412.41
5.4.150.0070.03512.40
5.4.140.0070.03812.10
5.4.130.0050.03512.07
5.4.120.0050.03612.04
5.4.110.0050.03512.04
5.4.100.0050.03612.04
5.4.90.0070.03912.04
5.4.80.0070.03712.04
5.4.70.0060.03412.04
5.4.60.0080.03212.03
5.4.50.0080.03812.03
5.4.40.0050.03512.02
5.4.30.0080.03512.02
5.4.20.0040.03512.01
5.4.10.0070.03312.02
5.4.00.0070.03911.51
5.3.280.0070.03612.71
5.3.270.0040.04112.73
5.3.260.0080.04112.72
5.3.250.0060.03812.72
5.3.240.0070.03612.72
5.3.230.0060.03712.71
5.3.220.0070.03512.68
5.3.210.0050.04212.68
5.3.200.0060.04012.68
5.3.190.0050.03812.68
5.3.180.0090.03312.67
5.3.170.0040.03812.67
5.3.160.0060.03612.67
5.3.150.0050.03812.67
5.3.140.0050.03812.66
5.3.130.0060.03812.66
5.3.120.0050.04112.66
5.3.110.0050.03912.66
5.3.100.0050.04312.13
5.3.90.0070.03812.11
5.3.80.0040.03912.11
5.3.70.0100.03312.10
5.3.60.0070.03912.09
5.3.50.0050.03712.04
5.3.40.0070.03512.04
5.3.30.0070.03212.00
5.3.20.0030.03811.78
5.3.10.0070.03411.75
5.3.00.0040.03811.73

preferences:
143.24 ms | 1386 KiB | 7 Q