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);

preferences:
23.93 ms | 402 KiB | 5 Q