Skip to content

Instantly share code, notes, and snippets.

@gkralik
Created April 12, 2017 09:02
Show Gist options
  • Save gkralik/3bc4a56fcd0f385899a00f951269f803 to your computer and use it in GitHub Desktop.
Save gkralik/3bc4a56fcd0f385899a00f951269f803 to your computer and use it in GitHub Desktop.
PHP: embed string in CDATA section
function embedInCData($str)
{
$dom = new \DOMDocument();
$cdata = $dom->createCDATASection($str);
return $dom->saveXML($cdata);
}
// usage:
echo embedInCData('<strong>foobar</strong>');
// <![CDATA[<strong>foobar</strong>]]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment