I am using php's DOMDocument->getElementById->nodeValue
to set a particular DOM element's HTML. The problem is that the string is converted to HTML entities:
eg:
nodeValue = html_entity_decode('<b>test</b>');
should output 'test' but instead it outputs '<b>test</b>
'
Any ideas why? This happens even if i don't use the html_entity_decode function
Here is my updated script...which is NOW working:
// Construct a DOM object for updating the affected node
$html = new DOMDocument("1.0", "utf-8");
if (!$html) return FALSE;
// Load the HTML file in question
$loaded = $html->loadHTMLFile($data['page_path']);
if (!$loaded)
{
print 'Failed to load file';
return FALSE;
}
// Establish the node being updated within the file
foreach ($data['divids'] as $divid)
{
$element = $html->getElementById($divid);
if (is_null($element))
{
print 'Failed to get existing element';
return FALSE;
}
$newelement = $html->createElement('div');
if (is_null($newelement))
{
print 'Failed to create new element';
return FALSE;
}
$newelement->setAttribute('id', $divid);
$newelement->setAttribute('class', 'reusable-block');
// Perform the replacement
$newelement->nodeValue = $replacement;
$parent = $element->parentNode;
$parent->replaceChild($newelement, $element);
// Save the file back to its location
$saved = $html->saveHTMLFile($data['page_path']);
if (!$saved)
{
print 'Failed to save file';
return FALSE;
}
}
// Replace HTML entities left over
$content = files::readFile($data['page_path']);
$content = str_replace('<', '<', $content);
$content = str_replace('>', '>', $content);
if (!@fwrite($handle, $content))
{
print 'Failed to replace entities';
return FALSE;
}