<?php
error_reporting(E_ALL | E_STRICT | E_NOTICE | E_WARNING);
ini_set('xdebug.default_enable', 'Off');
if (function_exists('xdebug_disable'))
{
    xdebug_disable();
}
define('STR_SUBJECT', 'subject');
define('STR_PATTERN', 'pattern');
define('STR_ACTION', 'action');
define('STR_MATCH_OFFSET_CAPTURE', 'offset_capture');
define('STR_SPLIT_DELIM_CAPTURE', 'delimit_capture');
$str_subject = (isset($_REQUEST[STR_SUBJECT])) ? $_REQUEST[STR_SUBJECT] : '';
$str_pattern = (isset($_REQUEST[STR_PATTERN])) ? $_REQUEST[STR_PATTERN] : '';
if (get_magic_quotes_gpc())
{
    $str_subject = stripslashes($str_subject);
    $str_pattern = stripslashes($str_pattern);
}
$bool_action = isset($_REQUEST[STR_ACTION]);
$bool_regexp_invalide = FALSE;
$str_message_erreur = '';
$int_preg_match = 0;
$str_preg_match = '';
$int_preg_match_all = 0;
$str_preg_match_all = '';
$str_preg_split = '';
if ($bool_action && strlen($str_pattern))
{
    ob_start();
    $int_preg_match = preg_match($str_pattern, '');
    $str_message_erreur = ob_get_clean();
    if ($str_message_erreur)
    {
        $str_message_erreur = htmlspecialchars(strip_tags($str_message_erreur));
        if (preg_match('/ ([^:]*) found in/U', $str_message_erreur, $arr_str_match))
        {
            $str_message_erreur = $arr_str_match[1];
        }
    }
    $bool_regexp_invalide = ($int_preg_match === FALSE);
    if (!$bool_regexp_invalide)
    {
        $int_preg_match = preg_match($str_pattern, $str_subject, $str_preg_match, (isset($_REQUEST[STR_MATCH_OFFSET_CAPTURE])) ? PREG_OFFSET_CAPTURE : 0);
        $int_preg_match_all = preg_match_all($str_pattern, $str_subject, $str_preg_match_all);
        $str_preg_split = preg_split($str_pattern, $str_subject, -1, ((isset($_REQUEST[STR_MATCH_OFFSET_CAPTURE])) ? PREG_SPLIT_OFFSET_CAPTURE : 0) | (isset($_REQUEST[STR_SPLIT_DELIM_CAPTURE])) ? PREG_SPLIT_DELIM_CAPTURE : 0);
        ob_start(); var_dump($str_preg_match); $str_preg_match = ob_get_clean();
        ob_start(); var_dump($str_preg_match_all); $str_preg_match_all = ob_get_clean();
        ob_start(); var_dump($str_preg_split); $str_preg_split = ob_get_clean();
    }
}
$str_subject = htmlspecialchars($str_subject);
$str_pattern = htmlspecialchars($str_pattern);
?>
<!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>Preg testeur</title>
        <style type="text/css">
        <!--
        body *
        {
            font-family: monospace;
            font-size: 8pt;
        }
        p.warning
        {
            color: FireBrick;
        }
        input.text, textarea
        {
            width: 600px;
        }
        textarea
        {
            height: 200px;
        }
        input.submit
        {
            width: 100px;
            display: block;
        }
        div.bloc
        {
            border: 1px dotted #ccc;
        }
        div.titre
        {
            font-weight: bold;
        }
        -->
        </style>
        <script type="text/javascript">
        <!--;
            function on_load()
            {
                if (document && document.getElementById)
                {
                    var obj_pattern = document.getElementById('<?php print STR_PATTERN; ?>');
                    if (obj_pattern && obj_pattern.focus)
                    {
                        obj_pattern.focus();
                    }
                }
            }
        //-->
        </script>
    </head>
    <body onload="on_load();">
        <form>
            <input type="hidden" name="<?php print STR_ACTION; ?>"  value="<?php print STR_ACTION; ?>" />
            <div class="ligne">
                <label for="<?php print STR_SUBJECT; ?>">Subject</label>
                <textarea class="textarea" name="<?php print STR_SUBJECT; ?>" id="<?php print STR_SUBJECT; ?>"><?php print $str_subject; ?></textarea>
            </div>
            <div class="ligne">
                <label for="<?php print STR_PATTERN; ?>">Pattern</label>
                <input type="text" class="text" name="<?php print STR_PATTERN; ?>" id="<?php print STR_PATTERN; ?>" value="<?php print $str_pattern; ?>"/>
            </div>
            <div class="ligne">
                <label for="">Option</label>
                <br />
                <input class="checkbox" type="checkbox" name="<?php print STR_MATCH_OFFSET_CAPTURE; ?>" id="<?php print STR_MATCH_OFFSET_CAPTURE; ?>" <?php print (isset($_REQUEST[STR_MATCH_OFFSET_CAPTURE])) ? 'checked="checked"' : ''; ?> />
                <label class="option" for="<?php print STR_MATCH_OFFSET_CAPTURE; ?>">PREG_OFFSET_CAPTURE</label>
                <br />
                <input class="checkbox" type="checkbox" name="<?php print STR_SPLIT_DELIM_CAPTURE; ?>" id="<?php print STR_SPLIT_DELIM_CAPTURE; ?>" <?php print (isset($_REQUEST[STR_SPLIT_DELIM_CAPTURE])) ? 'checked="checked"' : ''; ?> />
                <label class="option" for="<?php print STR_SPLIT_DELIM_CAPTURE; ?>">PREG_SPLIT_OFFSET_CAPTURE</label>
            </div>
            <div class="ligne">
                <input type="submit" class="submit" value="test" />
            </div>
        </form>
<?php
if ($bool_action)
{
    if ($bool_regexp_invalide)
    {
        if ($str_message_erreur)
        {
            $str_message_erreur = '(' . $str_message_erreur . ')';
        }
        printf('<p class="warning">Expression régulière invalide %s!</p>', $str_message_erreur);
    }
    else
    {
?>
        <div class="bloc">
            <div class="titre">
        <?php
            printf('PREG_MATCH (%s)', $int_preg_match);
        ?>
            </div>
            <div class="tableau">
        <?php
            printf('<pre>%s</pre>', htmlspecialchars($str_preg_match));
        ?>
            </div>
        </div>
        <div class="bloc">
            <div class="titre">
        <?php
            printf('PREG_SPLIT');
        ?>
            </div>
            <div class="tableau">
        <?php
            printf('<pre>%s</pre>', htmlspecialchars($str_preg_split));
        ?>
            </div>
        </div>
        <div class="bloc" onclick="alert(this.height);this.height='20px'">
            <div class="titre">
        <?php
            printf('PREG_MATCH_ALL (%s)', $int_preg_match_all);
        ?>
            </div>
            <div class="tableau">
        <?php
            printf('<pre>%s</pre>', htmlspecialchars($str_preg_match_all));
        ?>
            </div>
        </div>
<?php
    }
}
echo '<hr />';
highlight_file(__FILE__);
?>
    </body>
</html>