3v4l.org

run code in 300+ PHP versions simultaneously
<?php $DOM = new DOMDocument(); /** * Use internal errors to get around HTML5 warnings */ libxml_use_internal_errors(true); /** * Load in the content, with proper encoding and an `<html>` wrapper required for parsing */ $DOM->loadHTML("<?xml encoding='utf-8' ?><html>C.C. Johnson & Malhotra, P.C. (CCJM) was an integral member of a large Design Team for a 16.5-mile-long Public-Private Partnership (P3) Purple Line Project. The east-west light rail system extends from New Carrollton in PG County, MD to Bethesda in MO County, MD with 21 stations and one short tunnel. CCJM was Engineer of Record (EOR) for the design of eight (8) Bridges and design reviews for 35 transit/highway bridges and over 100 retaining walls of different lengths/types adjacent to bridges and in areas of cut/fill. CCJM designed utility structures for 42,000 LF of relocated water mains and 19,000 LF of relocated sewer mains meeting Washington Suburban Sanitary Commission (WSSC), Md Dept of Transportation (MDOT) MTA, and Local Standards.</html>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); /** * Clear errors to get around HTML5 warnings */ libxml_clear_errors(); /** * Initialize XPath */ $XPath = new DOMXPath($DOM); /** * Retrieve all text nodes, except those within scripts */ $text = $XPath->query("//text()[not(parent::script)]"); foreach ($text as $node) { /** * Find all matches, including offset */ preg_match_all("/(C\.? ?C\.?(?:JM| Johnson (?:&|&amp;|&#38;|and) Malhotra)(?: Engineers, LTD\.?|, P\.?C\.?)?)/i", $node->textContent, $matches, PREG_OFFSET_CAPTURE); /** * Wrap each match in appropriate span */ //foreach ($matches as $group) { $group = array_reverse($matches[0]); foreach ($group as $key => $match) { /** * Determine the offset and the length of the match */ $offset = $match[1]; $length = strlen($match[0]); /** * Isolate the match and what comes after it */ $word = $node->splitText($offset); $after = $word->splitText($length); /** * Create the wrapping span */ $span = $DOM->createElement("span"); $span->setAttribute("class", "__brand"); /** * Replace the word with the span, and then re-insert the word within it */ $word->parentNode->replaceChild($span, $word); $span->appendChild($word); //break; // it always errors after the first loop } //} } /** * Save changes, remove unneeded tags */ $content = implode(array_map([$DOM->documentElement->ownerDocument, "saveHTML"], iterator_to_array($DOM->documentElement->childNodes))); echo wordwrap($content);
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<p><span class="__brand">C.C. Johnson &amp; Malhotra, P.C.</span> (<span class="__brand">CCJM</span>) was an integral member of a large Design Team for a 16.5-mile-long Public-Private Partnership (P3) Purple Line Project. The east-west light rail system extends from New Carrollton in PG County, MD to Bethesda in MO County, MD with 21 stations and one short tunnel. <span class="__brand">CCJM</span> was Engineer of Record (EOR) for the design of eight (8) Bridges and design reviews for 35 transit/highway bridges and over 100 retaining walls of different lengths/types adjacent to bridges and in areas of cut/fill. <span class="__brand">CCJM</span> designed utility structures for 42,000 LF of relocated water mains and 19,000 LF of relocated sewer mains meeting Washington Suburban Sanitary Commission (WSSC), Md Dept of Transportation (MDOT) MTA, and Local Standards.</p>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <p><span class="__brand">C.C. Johnson &amp; Malhotra, P.C.</span> (<span class="__brand">CCJM</span>) was an integral member of a large Design Team for a 16.5-mile-long Public-Private Partnership (P3) Purple Line Project. The east-west light rail system extends from New Carrollton in PG County, MD to Bethesda in MO County, MD with 21 stations and one short tunnel. <span class="__brand">CCJM</span> was Engineer of Record (EOR) for the design of eight (8) Bridges and design reviews for 35 transit/highway bridges and over 100 retaining walls of different lengths/types adjacent to bridges and in areas of cut/fill. <span class="__brand">CCJM</span> designed utility structures for 42,000 LF of relocated water mains and 19,000 LF of relocated sewer mains meeting Washington Suburban Sanitary Commission (WSSC), Md Dept of Transportation (MDOT) MTA, and Local Standards.</p>

preferences:
137.41 ms | 403 KiB | 156 Q