File: regex_test.php

package info (click to toggle)
horde3 3.0.4-4sarge7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 15,980 kB
  • ctags: 16,295
  • sloc: php: 68,726; xml: 2,382; sql: 498; makefile: 74; sh: 63; pascal: 6
file content (146 lines) | stat: -rw-r--r-- 5,241 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
 * Simple script to help with regex construction/debugging. Put it
 * somewhere on your webserver and point your browser to it.
 *
 * Based on: http://myfluffydog.com/programming/php/scripts/regexp.php
 */

require 'Horde/Util.php';
$regexp = Util::getFormData('regexp');
$subject = Util::getFormData('subject');
$submit = Util::getFormData('submit');

@set_time_limit(2);

function unhtmlentities($string)
{
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}

if ($submit == "Test") { // doing a reg expression search -- get variables
    $unescapedRegexp = unhtmlentities($regexp); // for search
    $escapedRegexp = htmlentities($regexp); // for showing on page and in textarea
    $text = $subject;
    $text = stripslashes($text);
    $text = unhtmlentities($text); // for search
    $escapedText = htmlentities($text);// for textarea

    // Do preg_replace on source, substituting matched_text with [b]matched_text[/b]
    $highlightedText = preg_replace($unescapedRegexp, '[b]\\0[/b]', $text);
    // convert htmlentities
    $highlightedText = htmlentities($highlightedText);
    // replace [b] with <b> and [/b] with </b>
    $highlightedText = str_replace('[b]', '<span class="highlight">', $highlightedText);
    $highlightedText = str_replace('[/b]', '</span>', $highlightedText);
}
?>
<html>
<head>
<title>Test regular expressions</title>
<style type="text/css">
<!--
h1 {
    font-size: large;
}
.fixed {
    font-size: 10px;
    font-family: monospace,fixed;
    color: #000099;
    background-color: #cccccc;
}
.notes {
    font-size: small;
    color: #000099;
    background-color: #cccccc;
}
.highlight {
    color: #000066;
    background-color: #ffff00;
}
.empty {
    background-color: #ff9933
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<h1>Test <a href="http://www.php.net/manual/en/ref.pcre.php">PHP Regular Expression Functions (Perl-Compatible)</a></h1>
<div class="fixed">preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER)</div>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<p><strong>Enter your regular expression ($pattern):</strong><br />
<textarea name="regexp" cols="80" rows="6" id="regexp"><?php if($submit){echo $escapedRegexp;}?></textarea>
<br />
<strong>Enter text to search against in the textarea below ($subject):</strong><br />
<textarea name="subject" cols="80" rows="3"><?php if($subject){echo $escapedText;}?></textarea>
<br />
<input type="submit" name="submit" value="Test">
<input type="reset" name="Reset" value="Reset">
<a href='<?php echo $_SERVER['PHP_SELF'] ?>'>Home</a><br />
</p>
</form>
<hr>
<?php
if ($submit){ // only shows up on results page
    if (preg_match_all($unescapedRegexp, $text, $results, PREG_SET_ORDER)) { // found match
        echo "The regexp <strong> $escapedRegexp </strong> matched!\n<p>";
        echo "<h1>Here's what was matched searching the text you entered:</h1>\n";
        $y = 0;
        echo '<div class="fixed">';
        foreach ($results as $loop_result) {
            $x = 0;
            foreach ($loop_result as $loop_result2){
                if ($x != 0) {
                    /* Indent subpatterns. */
                    echo "<blockquote>";
                }
                printf('$matches[%s][%s]:&nbsp;&nbsp;', $y, $x);// show variable name
                if (!$loop_result2){
                    echo '<span class="empty">Empty</span>';
                } else {
                    echo '<span class="highlight">' . htmlentities($loop_result2) . '</span>';
                }
                if ($x != 0) {
                    echo "</blockquote>";
                } else {
                    echo "<p />";
                }
                $x++;
            }
            $y++;
        }
        echo '</div>';
    } else { // no match
        echo "regexp <strong> $escapedRegexp </strong> doesn't return any results\n<p>";
    }
    ?>
    <h1>Searched text with matches <span class='highlight'>highlighted</span></h1>
    <?php // Show searched text with matches highlighted
    echo '<pre>';
    echo $highlightedText;
    echo '</pre><hr>';
}
?>

<div class="notes">
<h1>Example regular expressions:</h1>
<p><strong>{&lt;!--(?:.|\s)*?--&gt;}</strong>&nbsp; --&nbsp; Shows comments in html&nbsp;</p>
<ul>
<li><strong>{</strong> open delimiter</li>
<li><strong>&lt;!--</strong> search for opening comment tag</li>
<li><strong>(?:.|\s)*</strong> any number of characters or white space</li>
<li><strong>?</strong> non-greedy</li>
<li><strong>--&gt;</strong> search for closing comment tag</li>
<li><strong>}</strong> close delimiter</li>
</ul>
<p><strong>{&lt;style type(?:.|\s)*?&lt;/style&gt;}</strong> -- Shows  styles</p>
<p>Notes:</p>
<ul>
<li>The results page shows the individual matches and subpattern matches, and the original text with the matches highlighted.</li>
<li>Might not be able to search for html entities like "&amp;amp;" because of the converting for display and textareas (the search pattern (regexp) and non-url subjects are processed by unhtmlentities)</li>
</ul></div>
</body>
</html>