<?php // $Id: xml.lib.php 9707 2007-12-12 13:46:31Z mlaurent $
if ( count( get_included_files() ) == 1 ) die( '---' );
/**
 * CLAROLINE
 * @version 1.8
 *
 * @copyright (c) 2001-2006 Universite catholique de Louvain (UCL)
 *
 * @license http://www.gnu.org/copyleft/gpl.html (GPL) GENERAL PUBLIC LICENSE
 *
 * @author claro team <cvs@claroline.net>
 */

/**
 * transform all entities of $string to their hexadecimal representation
 *
 * @param $string string original string to entify
 * @param $quote_style string Constant choosen among ENT_COMPAT, ENT_QUOTES OR ENT_NOQUOTES (see htmlentities doc for more info)
 *
 * @return string entified string
 */
function xmlentities( $string, $quote_style = ENT_QUOTES )
{
    static $trans;

	// remove all html entities before xml encoding
	// must convert all quotes to avoid remaining html entity in code
	$string = html_entity_decode($string, ENT_QUOTES);

	// xml encoding
    if ( ! isset( $trans ) )
	{
        $trans = get_html_translation_table( HTML_ENTITIES, $quote_style );
        foreach ( $trans as $key => $value )
        {
            $trans[$key] = '&#'.ord( $key ).';';
        }
        // dont translate the '&' in case it is part of &xxx;
        $trans[chr(38)] = '&';
	}

    // after the initial translation, _do_ map standalone '&' into '&#38;'
    return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/"
        , "&#38;"
        , strtr( $string, $trans )
        );
}

?>