src/XML/Util.php
function replaceEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML,
        $encoding = 'ISO-8859-1')
    {
        switch ($replaceEntities) {
        case XML_UTIL_ENTITIES_XML:
            return strtr($string, array(
                '&'  => '&amp;',
                '>'  => '&gt;',
                '<'  => '&lt;',
                '"'  => '&quot;',
                '\'' => '&apos;' ));
            break;
        case XML_UTIL_ENTITIES_XML_REQUIRED:
            return strtr($string, array(
                '&' => '&amp;',
                '<' => '&lt;',
                '"' => '&quot;' ));
            break;
        case XML_UTIL_ENTITIES_HTML:
            return htmlentities($string, ENT_COMPAT, $encoding);
            break;
        }
        return $string;
    }
function replaceEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML,
        $encoding = 'ISO-8859-1')
    {
        switch ($replaceEntities) {
        case XML_UTIL_ENTITIES_XML:
            return strtr($string, array(
                '&'  => '&amp;',
                '>'  => '&gt;',
                '<'  => '&lt;',
                '"'  => '&quot;',
                '\'' => '&apos;' ));
            break;
        case XML_UTIL_ENTITIES_XML_REQUIRED:
            return strtr($string, array(
                '&' => '&amp;',
                '<' => '&lt;',
                '"' => '&quot;' ));
            break;
        case XML_UTIL_ENTITIES_HTML:
            return htmlentities($string, ENT_COMPAT, $encoding);
            break;
        }
        return $string;
    }
replace XML entities

With the optional second parameter, you may select, which
entities should be replaced.

<code>
require_once 'XML/Util.php';

// replace XML entites:
$string = XML_Util::replaceEntities('This string contains < & >.');
</code>

With the optional third parameter, you may pass the character encoding
<code>
require_once 'XML/Util.php';

// replace XML entites in UTF-8:
$string = XML_Util::replaceEntities(
'This string contains < & > as well as ä, ö, ß, à and ê',
XML_UTIL_ENTITIES_HTML,
'UTF-8'
);
</code>

@param string $string          string where XML special chars
should be replaced
@param int    $replaceEntities setting for entities in attribute values
(one of XML_UTIL_ENTITIES_XML,
XML_UTIL_ENTITIES_XML_REQUIRED,
XML_UTIL_ENTITIES_HTML)
@param string $encoding        encoding value (if any)...
must be a valid encoding as determined
by the htmlentities() function

@return string string with replaced chars
@access public
@static
@see reverseEntities()