3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for git.master, git.master_jit, rfc.property-hooks
<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>

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
36.96 ms | 402 KiB | 8 Q