<?php
/**
 * @file formater_nametoken.php
 * @brief Page qui transforme un intitulé en nametoken.
 *
 * @author hughes monget
 * @see http://monget.com/
 */
define('STR_TEXTE',   'texte');
define('STR_ACTION',  'action');
$bool_action = isset($_REQUEST[STR_ACTION]) && isset($_REQUEST[STR_TEXTE]);
$str_texte    = '';
$str_formater = '';
if ($bool_action)
{
    $str_texte = $_REQUEST[STR_TEXTE];
    if (get_magic_quotes_gpc())
    {
        $str_texte = stripslashes($str_texte);
    }
    $str_formater = $str_texte;
    // On remplace les caractères accentués par leur équivalent ascii.
    // http://www.phpsources.org/scripts84-PHP.htm
    $str_from = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ';
    $str_to   = 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn';
    /* $str_formater = strtr($str_formater, $str_from,    $str_to); */
    $str_from = str_split($str_from);
    $str_to   = str_split($str_to);
    $str_formater = str_replace($str_from, $str_to, $str_formater);
    // On remplace tous les caractères non autorisés par underscore.
    $str_formater = preg_replace('/[^a-zA-Z0-9_]+/', '_', $str_formater);
    // On remplace les underscores consécutifs par un seul.
    $str_formater = preg_replace('/__+/', '_', $str_formater);
    // On supprime les underscores en début et fin.
    $str_formater = preg_replace('/^_|_$/', '', $str_formater);
    // On minuscule.
    $str_formater = strtolower($str_formater);
    $str_texte    = htmlspecialchars($str_texte);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Formater un texte en nametoken</title>
        <style type="text/css">
        <!--
        body *
        {
            font-family: monospace;
            font-size: 8pt;
        }
        input
        {
            display: block;
        }
        input.text
        {
            width: 800px;
        }
        form
        {
            margin: 0;
        }
        -->
        </style>
        <script type="text/javascript">
        <!--;
            function on_load()
            {
                if (document && document.getElementById)
                {
                    var obj_pattern = document.getElementById('<?php print STR_TEXTE; ?>');
                    if (obj_pattern && obj_pattern.focus)
                    {
                        obj_pattern.focus();
                    }
                }
            }
        //-->
        </script>
    </head>
    <body onload="on_load();">
        <form method="post">
            <input type="hidden" name="<?php echo STR_ACTION; ?>" value="<?php echo STR_ACTION; ?>" />
            <input class="text" type="text" name="<?php echo STR_TEXTE; ?>" value="<?php echo $str_texte; ?>" />
            <input type="submit" class="submit" value="formater" />
        </form>
<?php
if ($bool_action)
{
    printf('<input class="text" type="text" value="%s" onclick="this.select();" readonly="readonly" />', $str_formater);
}
echo '<hr />';
highlight_file(__FILE__);
?>
    </body>
</html>