Using PHP I\'m attempting to take an HTML string passed from a WYSIWYG editor and replace the children of an element inside of a preloaded HTML document with the new HTML.>
I know this is an old thread (but reply on this because also looking for a solution to this). I have made an easy method to replace content with just one single line when using it. To understand the method better, I also add some context named functions.
This is now a part of my library, so that's the reason of all function names here, all functions starts with the prefix 'su'.
It is very easy to use and very powerful (and quite less code).
Here is the code:
function suSetHtmlElementById( &$oDoc, &$s, $sId, $sHtml, $bAppend = false, $bInsert = false, $bAddToOuter = false )
{
if( suIsValidString( $s ) && suIsValidString( $sId ))
{
$bCreate = true;
if( is_object( $oDoc ))
{
if( !( $oDoc instanceof DOMDocument ))
{ return false; }
$bCreate = false;
}
if( $bCreate )
{ $oDoc = new DOMDocument(); }
libxml_use_internal_errors(true);
$oDoc->loadHTML($s);
libxml_use_internal_errors(false);
$oNode = $oDoc->getElementById( $sId );
if( is_object( $oNode ))
{
$bReplaceOuter = ( !$bAppend && !$bInsert );
$sId = uniqid('SHEBI-');
$aId = array( "", "" );
if( $bReplaceOuter )
{
if( suIsValidString( $sHtml ) )
{
$oNode->parentNode->replaceChild( $oDoc->createComment( $sId ), $oNode );
$s = $oDoc->saveHtml();
$s = str_replace( $aId, $sHtml, $oDoc->saveHtml());
}
else { $oNode->parentNode->removeChild( $oNode );
$s = $oDoc->saveHtml();
}
return true;
}
$bReplaceInner = ( $bAppend && $bInsert );
$sThis = null;
if( !$bReplaceInner )
{
$sThis = $oDoc->saveHTML( $oNode );
$sThis = ($bInsert?$sHtml:'').($bAddToOuter?$sThis:(substr($sThis,strpos($sThis,'>')+1,-(strlen($oNode->nodeName)+3)))).($bAppend?$sHtml:'');
}
if( !$bReplaceInner && $bAddToOuter )
{
$oNode->parentNode->replaceChild( $oDoc->createComment( $sId ), $oNode );
$sId = &$aId;
}
else { $oNode->nodeValue = $sId; }
$s = str_replace( $sId, $bReplaceInner?$sHtml:$sThis, $oDoc->saveHtml());
return true;
}
}
return false;
}
// A function of my library used in the function above:
function suIsValidString( &$s, &$iLen = null, $minLen = null, $maxLen = null )
{
if( !is_string( $s ) || !isset( $s{0} ))
{ return false; }
if( $iLen !== null )
{ $iLen = strlen( $s ); }
return (( $minLen===null?true:($minLen > 0 && isset( $s{$minLen-1} ))) &&
$maxLen===null?true:($maxLen >= $minLen && !isset( $s{$maxLen})));
}
Some context functions:
function suAppendHtmlById( &$s, $sId, $sHtml, &$oDoc = null )
{ return suSetHtmlElementById( $oDoc, $s, $sId, $sHtml, true, false ); }
function suInsertHtmlById( &$s, $sId, $sHtml, &$oDoc = null )
{ return suSetHtmlElementById( $oDoc, $s, $sId, $sHtml, false, true ); }
function suAddHtmlBeforeById( &$s, $sId, $sHtml, &$oDoc = null )
{ return suSetHtmlElementById( $oDoc, $s, $sId, $sHtml, false, true, true ); }
function suAddHtmlAfterById( &$s, $sId, $sHtml, &$oDoc = null )
{ return suSetHtmlElementById( $oDoc, $s, $sId, $sHtml, true, false, true ); }
function suSetHtmlById( &$s, $sId, $sHtml, &$oDoc = null )
{ return suSetHtmlElementById( $oDoc, $s, $sId, $sHtml, true, true ); }
function suReplaceHtmlElementById( &$s, $sId, $sHtml, &$oDoc = null )
{ return suSetHtmlElementById( $oDoc, $s, $sId, $sHtml, false, false ); }
function suRemoveHtmlElementById( &$s, $sId, &$oDoc = null )
{ return suSetHtmlElementById( $oDoc, $s, $sId, null, false, false ); }
How to use it:
In the following examples, I assume that there is already content loaded into a variable called $sMyHtml
and the variable $sMyNewContent
contains some new html. The variable $sMyHtml
contains an element called/with the id 'example_id
'.
// Example 1: Append new content to the innerHTML of an element (bottom of element):
if( suAppendHtmlById( $sMyHtml, 'example_id', $sMyNewContent ))
{ echo $sMyHtml; }
else { echo 'Element not found?'; }
// Example 2: Insert new content to the innerHTML of an element (top of element):
suInsertHtmlById( $sMyHtml, 'example_id', $sMyNewContent );
// Example 3: Add new content ABOVE element:
suAddHtmlBeforeById( $sMyHtml, 'example_id', $sMyNewContent );
// Example 3: Add new content BELOW/NEXT TO element:
suAddHtmlAfterById( $sMyHtml, 'example_id', $sMyNewContent );
// Example 4: SET new innerHTML content of element:
suSetHtmlById( $sMyHtml, 'example_id', $sMyNewContent );
// Example 5: Replace entire element with new content:
suReplaceHtmlElementById( $sMyHtml, 'example_id', $sMyNewContent );
// Example 6: Remove entire element:
suSetHtmlElementById( $sMyHtml, 'example_id' );