I need to extend the functionality of the following code snippet to convert spaces -only- between PRE tags in a string containing html:
str_replace(\' \',\'&
You can use a DOM parser. Here is how you would do it using PHP native DOM functions:
abc 123
abcedfg 12345
abc 123
abcedfg 12345
abcedfg 12345
';
$dom = new DOMDocument("1.0");
$dom->loadHTML($test);
$xpath = new DOMXpath($dom);
$pre = $xpath->query("//pre");
foreach($pre as $e) {
$e->nodeValue = str_replace(" ", " ", $e->nodeValue);
}
echo $dom->saveHTML();
abc 123
abcedfg 12345
abc 123
abcedfg 12345
abcedfg 12345
I am not sure how to get rid of doctype/html/body tags. One possible solution that works on PHP >= 5.3.6 is to specify which node to output in the saveHTML() method. Other possibility is to use regex which I have avoided in the first place.