3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Html{ protected $reachedLimit = false, $totalLen = 0, $maxLen = 25, $toRemove = array(); public static function trim($html, $maxLen = 25){ $dom = new DomDocument(); $dom->loadHTML($html); $html = new static(); $toRemove = $html->walk($dom, $maxLen); // remove any nodes that passed our limit foreach($toRemove as $child) $child->parentNode->removeChild($child); // remove wrapper tags added by DD (doctype, html...) if(version_compare(PHP_VERSION, '5.3.6') < 0){ // http://stackoverflow.com/a/6953808/1058140 $dom->removeChild($dom->firstChild); $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); return $dom->saveHTML(); } return $dom->saveHTML($dom->getElementsByTagName('body')->item(0)); } protected function walk(DomNode $node, $maxLen){ if($this->reachedLimit){ $this->toRemove[] = $node; }else{ // only text nodes should have text, // so do the splitting here if($node instanceof DomText){ $this->totalLen += $nodeLen = strlen($node->nodeValue); // use mb_strlen / mb_substr for UTF-8 support if($this->totalLen > $maxLen){ $node->nodeValue = substr($node->nodeValue, 0, $nodeLen - ($this->totalLen - $maxLen)) . '...'; $this->reachedLimit = true; } } // if node has children, walk its child elements if(isset($node->childNodes)) foreach($node->childNodes as $child) $this->walk($child, $maxLen); } return $this->toRemove; } } print html::trim('<p id="mct_ai_excerpt">The <a class="zem_slink" title="Class action" href="http://en.wikipedia.org/wiki/Class_action" target="_blank" rel="wikipedia">class action</a> Complaint was filed by the San Francisco labor law lawyers at Blumenthal, Nordrehaug &amp; Bhowmik. The lawsuit, filed on July 17, 2014, alleges that Kmart Corporation misclassified their Loss Prevention Managers as exempt from overtime pay and as a result, failed to pay overtime wages, and failed to provide meal and rest breaks, as required by the California Labor Code. The lawsuit entitled, Moua, et al. v. Kmart Corporation &amp; Sears Holdings Management Corporation, Case No. 14CECG02033 is currently pending in the Fresno County Superior Court for the State of California. The Kmart Corporation complaint alleges that the Plaintiff, who worked as a Loss Prevention Manager, engaged in non-exempt tasks throughout his workday, including ensuring the proper operation and repairs of alarm equipment, detaining and processing shoplifters, and observing and reviewing store <a class="zem_slink" title="Security Camera Videos" href="http://www.break.com/topics/security-camera-videos" target="_blank" rel="break">surveillance cameras</a> among other things. The Complaint further claims that the Plaintiff and other Loss Prevention Managers are allegedly "managers in name only" as they do not direct the work of other employees and spend most of their day engaging in non-exempt job tasks. As such, the Complaint claims these employees have been misclassified under California law and seeks an unspecified amount of back overtime pay for the Plaintiff and the proposed class. The firm also represents a class of Loss Prevention Managers who worked for Frys Electronics claiming alleged unpaid overtime wages, Steffan v. Frys Electronics, Inc. That case is pending in the <a class="zem_slink" title="Santa Clara County Superior Court" href="http://www.sccsuperiorcourt.org/" target="_blank" rel="homepage">Santa Clara County Superior Court</a>, Case No. 1-13-CV-2541011.</p>', 150);
based on 7Ueck
Output for 5.3.6 - 7.1.0
<body><p id="mct_ai_excerpt">The <a class="zem_slink" title="Class action" href="http://en.wikipedia.org/wiki/Class_action" target="_blank" rel="wikipedia">class action</a> Complaint was filed by the San Francisco labor law lawyers at Blumenthal, Nordrehaug &amp; Bhowmik. The lawsuit, filed on July 17, 2014, ...</p></body>
Output for 5.3.0 - 5.3.5
<p id="mct_ai_excerpt">The <a class="zem_slink" title="Class action" href="http://en.wikipedia.org/wiki/Class_action" target="_blank" rel="wikipedia">class action</a> Complaint was filed by the San Francisco labor law lawyers at Blumenthal, Nordrehaug &amp; Bhowmik. The lawsuit, filed on July 17, 2014, ...</p>
Output for 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_STATIC, expecting T_STRING or T_VARIABLE or '$' in /in/RBOJI on line 18
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STATIC, expecting T_STRING or T_VARIABLE or '$' in /in/RBOJI on line 18
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/RBOJI on line 7
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/RBOJI on line 7
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/RBOJI on line 7
Process exited with code 255.