[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/util/ -> regex_test.php (source)

   1  <?php
   2  /**
   3   * Simple script to help with regex construction/debugging. Put it
   4   * somewhere on your webserver and point your browser to it.
   5   *
   6   * Based on: http://myfluffydog.com/programming/php/scripts/regexp.php
   7   *
   8   * $Horde: horde/util/regex_test.php,v 1.3.10.4 2005/10/18 11:34:21 jan Exp $
   9   */
  10  
  11  require  'Horde/Util.php';
  12  $regexp = Util::getFormData('regexp');
  13  $subject = Util::getFormData('subject');
  14  $submit = Util::getFormData('submit');
  15  
  16  @set_time_limit(2);
  17  
  18  function unhtmlentities($string)
  19  {
  20      $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  21      $trans_tbl = array_flip($trans_tbl);
  22      return strtr($string, $trans_tbl);
  23  }
  24  
  25  if ($submit == "Test") { // doing a reg expression search -- get variables
  26      $unescapedRegexp = unhtmlentities($regexp); // for search
  27      $escapedRegexp = htmlentities($regexp); // for showing on page and in textarea
  28      $text = $subject;
  29      $text = stripslashes($text);
  30      $text = unhtmlentities($text); // for search
  31      $escapedText = htmlentities($text);// for textarea
  32  
  33      // Do preg_replace on source, substituting matched_text with [b]matched_text[/b]
  34      $highlightedText = preg_replace($unescapedRegexp, '[b]\\0[/b]', $text);
  35      // convert htmlentities
  36      $highlightedText = htmlentities($highlightedText);
  37      // replace [b] and [/b] with highlight.
  38      $highlightedText = str_replace('[b]', '<span class="highlight">', $highlightedText);
  39      $highlightedText = str_replace('[/b]', '</span>', $highlightedText);
  40  }
  41  ?>
  42  <html>
  43  <head>
  44  <title>Test regular expressions</title>
  45  <style type="text/css">
  46  <!--
  47  h1 {
  48      font-size: large;
  49  }
  50  .fixed {
  51      font-size: 10px;
  52      font-family: monospace,fixed;
  53      color: #000099;
  54      background-color: #cccccc;
  55  }
  56  .notes {
  57      font-size: small;
  58      color: #000099;
  59      background-color: #cccccc;
  60  }
  61  .highlight {
  62      color: #000066;
  63      background-color: #ffff00;
  64  }
  65  .empty {
  66      background-color: #ff9933
  67  }
  68  -->
  69  </style>
  70  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  71  </head>
  72  <body bgcolor="#FFFFFF">
  73  <h1>Test <a href="http://www.php.net/manual/en/ref.pcre.php">PHP Regular Expression Functions (Perl-Compatible)</a></h1>
  74  <div class="fixed">preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER)</div>
  75  <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
  76  <p><strong>Enter your regular expression ($pattern):</strong><br />
  77  <textarea name="regexp" cols="80" rows="6" id="regexp"><?php if($submit){echo $escapedRegexp;}?></textarea>
  78  <br />
  79  <strong>Enter text to search against in the textarea below ($subject):</strong><br />
  80  <textarea name="subject" cols="80" rows="3"><?php if($subject){echo $escapedText;}?></textarea>
  81  <br />
  82  <input type="submit" name="submit" value="Test">
  83  <input type="reset" name="Reset" value="Reset">
  84  <a href='<?php echo $_SERVER['PHP_SELF'] ?>'>Home</a><br />
  85  </p>
  86  </form>
  87  <hr />
  88  <?php
  89  if ($submit) { // only shows up on results page
  90      if (preg_match_all($unescapedRegexp, $text, $results, PREG_SET_ORDER)) { // found match
  91          echo "The regexp <strong> $escapedRegexp </strong> matched!\n<p>";
  92          echo "<h1>Here's what was matched searching the text you entered:</h1>\n";
  93          $y = 0;
  94          echo '<div class="fixed">';
  95          foreach ($results as $loop_result) {
  96              $x = 0;
  97              foreach ($loop_result as $loop_result2) {
  98                  if ($x != 0) {
  99                      /* Indent subpatterns. */
 100                      echo "<blockquote>";
 101                  }
 102                  printf('$matches[%s][%s]:&nbsp;&nbsp;', $y, $x);// show variable name
 103                  if (!$loop_result2) {
 104                      echo '<span class="empty">Empty</span>';
 105                  } else {
 106                      echo '<span class="highlight">' . htmlentities($loop_result2) . '</span>';
 107                  }
 108                  if ($x != 0) {
 109                      echo "</blockquote>";
 110                  } else {
 111                      echo "<p />";
 112                  }
 113                  $x++;
 114              }
 115              $y++;
 116          }
 117          echo '</div>';
 118      } else { // no match
 119          echo "regexp <strong> $escapedRegexp </strong> doesn't return any results\n<p>";
 120      }
 121      ?>
 122      <h1>Searched text with matches <span class='highlight'>highlighted</span></h1>
 123      <?php // Show searched text with matches highlighted
 124      echo '<pre>';
 125      echo $highlightedText;
 126      echo '</pre><hr />';
 127  }
 128  ?>
 129  
 130  <div class="notes">
 131  <h1>Example regular expressions:</h1>
 132  <p><strong>{&lt;!--(?:.|\s)*?--&gt;}</strong>&nbsp; --&nbsp; Shows comments in html&nbsp;</p>
 133  <ul>
 134  <li><strong>{</strong> open delimiter</li>
 135  <li><strong>&lt;!--</strong> search for opening comment tag</li>
 136  <li><strong>(?:.|\s)*</strong> any number of characters or white space</li>
 137  <li><strong>?</strong> non-greedy</li>
 138  <li><strong>--&gt;</strong> search for closing comment tag</li>
 139  <li><strong>}</strong> close delimiter</li>
 140  </ul>
 141  <p><strong>{&lt;style type(?:.|\s)*?&lt;/style&gt;}</strong> -- Shows  styles</p>
 142  <p>Notes:</p>
 143  <ul>
 144  <li>The results page shows the individual matches and subpattern matches, and the original text with the matches highlighted.</li>
 145  <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>
 146  </ul></div>
 147  </body>
 148  </html>


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7