3v4l.org

run code in 300+ PHP versions simultaneously
<?php $s = '<p>I am <30 years old and weight <12st</p>'; $libxmlErrorState = libxml_use_internal_errors(true); function getLastErrorPos($code) { $errors = array_filter(libxml_get_errors(), function ($e) use ($code) { return $e->code === $code; }); if ( !$errors ) return false; $lastError = array_pop($errors); return ['line' => $lastError->line - 1, 'column' => $lastError->column - 2 ]; } define('XML_ERR_NAME_REQUIRED', 68); // xmlParseEntityRef: no name $patternTemplate = '~(?:.*\R){%d}.{%d}\K<~A'; $dom = new DOMDocument; $dom->loadHTML($s, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); while ( false !== $position = getLastErrorPos(XML_ERR_NAME_REQUIRED) ) { libxml_clear_errors(); $pattern = vsprintf($patternTemplate, $position); $s = preg_replace($pattern, '&lt;', $s, 1); $dom = new DOMDocument; $dom->loadHTML($s, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); } echo $dom->saveHTML(); libxml_clear_errors(); libxml_use_internal_errors($libxmlErrorState);
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.20, 8.3.0 - 8.3.8
<p>I am &lt;30 years old and weight &lt;12st</p>

preferences:
174.81 ms | 403 KiB | 188 Q