3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Html{ protected $reachedLimit = false, $totalLen = 0, $maxLen = 25; public static function trim($html, $maxLen = 25){ $dom = new DomDocument(); $dom->loadHTML($html); $html = new static(); $html->walk($dom, $maxLen); // remove wrapper tags added by DD (doctype, html...) // http://stackoverflow.com/a/6953808/1058140 $dom->removeChild($dom->firstChild); $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); return $dom->saveHTML(); } protected function walk(DomNode $node, $maxLen){ // remove any nodes that passed our limit if($this->reachedLimit){ $node->parentNode->removeChild($node); return; } // only text nodes should have text, // so do the splitting here if($node instanceof DomText){ $this->totalLen += $nodeLen = strlen($node->nodeValue); if($this->totalLen > $maxLen){ $node->nodeValue = substr($node->nodeValue, 0, $nodeLen - ($this->totalLen - $maxLen)); $this->reachedLimit = true; } } if(!$node->hasChildNodes()) return; // if node has children, walk its child elements foreach($node->childNodes as $child) $this->walk($child, $maxLen); } } print html::trim('<p>I can haz <strong> html trim </strong>, 14);

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.150.0210.04812.39
5.4.140.0130.04312.08
5.4.130.0160.03912.06
5.4.120.0190.05612.03
5.4.110.0150.03812.02
5.4.100.0110.04212.02
5.4.90.0190.04612.02
5.4.80.0110.04312.02
5.4.70.0120.04112.02
5.4.60.0110.04112.02
5.4.50.0180.05512.02
5.4.40.0150.04912.01
5.4.30.0130.04212.00
5.4.20.0110.05012.00
5.4.10.0110.04212.00
5.4.00.0150.04711.50
5.3.250.0120.03812.72
5.3.240.0110.04412.72
5.3.230.0130.04412.71
5.3.220.0130.04212.68
5.3.210.0180.04812.68
5.3.200.0140.04312.68
5.3.190.0140.05512.68
5.3.180.0100.04412.67
5.3.170.0130.04212.67
5.3.160.0130.04412.67
5.3.150.0210.05312.67
5.3.140.0150.04012.66
5.3.130.0110.04412.66
5.3.120.0110.04412.66
5.3.110.0160.04012.66
5.3.100.0120.04512.13
5.3.90.0130.04112.10
5.3.80.0160.04912.09
5.3.70.0120.04212.09
5.3.60.0120.04212.08
5.3.50.0100.04312.02
5.3.40.0140.04912.02
5.3.30.0160.04611.99
5.3.20.0110.04211.77
5.3.10.0130.03911.73
5.3.00.0120.04111.72

preferences:
138.57 ms | 1399 KiB | 7 Q