3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * DOM xpath to find #text nodes and wrap in paragraph tag * @link http://stackoverflow.com/q/15552560/367456 */ $html = '<div> This text should be wrapped in a p tag. </div> This also should be wrapped. <b>And</b> this.'; $paragraphSequence = "\n\n"; $paragraphSequenceLength = 2; $paragraphComment = 'break'; /** * @param DOMNode $element * * @link http://php.net/book.dom * @return string */ $innerHTML = function (DOMNode $element) { $innerHTML = ""; $children = $element->childNodes; foreach ($children as $child) { $innerHTML .= $element->ownerDocument->saveHTML($child); } return $innerHTML; }; /** * @param $html * * @return DOMElement * @throws InvalidArgumentException */ $loadHTMLFragment = function ($html) { $doc = new DOMDocument(); $doc->loadHTML("<body>$html"); $anchor = $doc->getElementsByTagName('body')->item(0); if (!$anchor) { throw new InvalidArgumentException('Unable to load HTML fragment.'); } return $anchor; }; $insertBreakMarkBefore = function (DOMText $node, $paragraphSequenceBefore = FALSE) use ($paragraphSequence, $paragraphComment) { if ($paragraphSequenceBefore) { $node->parentNode->insertBefore($node->ownerDocument->createTextNode($paragraphSequence), $node); } $node->parentNode->insertBefore($node->ownerDocument->createComment($paragraphComment), $node); $node->parentNode->insertBefore($node->ownerDocument->createTextNode($paragraphSequence), $node); }; $anchor = $loadHTMLFragment($html); $xp = new DOMXPath($anchor->ownerDocument); /* @var $result DOMText[] */ $result = $xp->query('(.|./div)/text()', $anchor); foreach ($result as $i => $node) { if ($node->parentNode->tagName == 'div') { $insertBreakMarkBefore($node, true); } while (FALSE !== $pos = strpos($node->data, $paragraphSequence)) { $node = $node->splitText($pos + $paragraphSequenceLength); $insertBreakMarkBefore($node); } } $needle = sprintf('%1$s<!--%2$s-->%1$s', $paragraphSequence, $paragraphComment); $replace = sprintf("\n<p class=\"%s\">\n", $paragraphComment); $html = strtr($innerHTML($anchor), array($needle . $needle => $replace, $needle => $replace)); echo "HTML afterwards:\n", $innerHTML($loadHTMLFragment($html));
Output for 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
HTML afterwards: <div> <p class="break"> This text should be wrapped in a p tag. </p></div> <p class="break"> This also should be wrapped. </p><p class="break"> <b>And</b> this.</p>
Output for 5.3.6 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.35, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.29 - 7.2.33
HTML afterwards: <div> <p class="break"> This text should be wrapped in a p tag. </p> </div> <p class="break"> This also should be wrapped. </p> <p class="break"> <b>And</b> this.</p>
Output for 5.3.0 - 5.3.5
Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/6fj1b on line 32 HTML afterwards:
Output for 5.2.3 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/6fj1b on line 25
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.2
<br /> <b>Parse error</b>: syntax error, unexpected T_FUNCTION in <b>/in/6fj1b</b> on line <b>25</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
<br /> <b>Parse error</b>: parse error, unexpected T_FUNCTION in <b>/in/6fj1b</b> on line <b>25</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/6fj1b</b> on line <b>25</b><br />
Process exited with code 255.

preferences:
190.02 ms | 401 KiB | 338 Q