<?php
/**
 * @file decoder_charset_inconnu.php.php
 * @brief Page qui sert à détecter les caractères de formatage dans une chaîne pour fprintf.
 *
 * @author hughes monget
 * @see http://monget.com/
 */
define('STR_TEXTE',   'texte');
define('STR_ACTION',  'action');
define('REGEXP_FORMAT', '/%[ \d+hlL*#.-]*[diouxXfeEgGcsp%]/U');
$bool_action = isset($_REQUEST[STR_ACTION]) && isset($_REQUEST[STR_TEXTE]) && is_scalar($_REQUEST[STR_ACTION]);
$str_texte     = '%d  %0.2d %% %f %s
[%10s] [%-10s] [%010s] [%\'#10s] [%10.10s]
%toto
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
$formatted = sprintf("%01.2f", $money);
echo sprintf("%.3e", $number);';
if ($bool_action)
{
    $str_texte = $_REQUEST[STR_TEXTE];
    if (get_magic_quotes_gpc())
    {
        $str_texte = stripslashes($str_texte);
    }
    $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>détecter un % dans une chaîne pour printf</title>
        <style type="text/css">
        <!--
        body *
        {
            font-family: monospace;
            font-size: 8pt;
        }
        textarea
        {
            height: 200px;
            width: 600px;
        }
        input.submit
        {
            width: 100px;
            display: block;
        }
        input#<?php print STR_WRAP; ?>
        {
            width: 50px;
        }
        .outline
        {
            background: #FF0;        }
        -->
        </style>
        <script type="text/javascript">
        <!--;
            function on_load()
            {
                if (document && document.getElementById)
                {
                    var obj_pattern = document.getElementById('<?php print STR_MAIL; ?>');
                    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; ?>" />
            <textarea id="<?php print STR_TEXTE; ?>" name="<?php print STR_TEXTE; ?>"><?php print $str_texte; ?></textarea>
            <input type="submit" class="submit" value="détecter" />
        </form>
<?php
if ($bool_action)
{
    // Cf syntaxe: http://membres.multimania.fr/dancel/c/c60_10.htm
    $arr_str_pourcent = array();
    if (preg_match_all(REGEXP_FORMAT, $str_texte, $arr_arr_str_match))
    {
        $arr_str_pourcent = $arr_arr_str_match[0];
        //natcasesort($arr_str_pourcent);
    }
    if (!$arr_str_pourcent)
    {
        echo 'Aucune % trouvé.';
    }
    else
    {
        echo '<pre>';
        echo implode(" ", $arr_str_pourcent);
        echo '</pre>';
    }
    echo '<hr /><pre>'.preg_replace(REGEXP_FORMAT, '<span class="outline">\0</span>', $str_texte).'</pre>';
}
echo '<hr />';
highlight_file(__FILE__);
?>
    </body>
</html>