source: scrabutility/www/lookup.php @ 100

Revision 98, 2.7 KB checked in by csnyder, 11 years ago (diff)

Rearranging for proper release

Line 
1<?php
2
3// for quoting
4function h( $text ) {
5  return htmlentities( $text, ENT_QUOTES, 'utf-8' );
6}
7
8// system grep
9$grep = '/bin/grep';
10$dictionary = './'.$dict.'.txt';
11
12?>
13<div style="width: 67%; margin: 6px auto;">
14<p>
15Enter letters to find matches in the word list.
16Use <code>.</code> (dot) for wildcard.<br>
17Use <code>[</code> and <code>]</code> to match the beginning and/or the ending of a word.</p>
18<form method="get">
19<p><input type="text" name="q" value="<?=h($_GET['q'])?>" size="24"><input type="submit" class="button" value="lookup"></p>
20</form>
21<?
22if( !empty($_GET['q']) ){
23  try {
24    $q = trim( strtoupper( $_GET['q'] ) );
25    // allow A-Z, . (dot), [, and ]
26    if ( preg_match('/[^A-Z\.\[\]]/', $q) ) {
27      throw new Exception( "Invalid search term(s). Please use letters a-z, or one of the codes listed above." );
28    }
29    // force [ at the beginning, and ] at the end
30    if ( strpos($q, '[') ) {
31      throw new Exception( "You must use the word-beginning code [ at the beginning of your search terms." );
32    }
33    if ( strpos($q, ']') && substr( $q, -1) != ']' ) {
34      throw new Exception( "You must use the word-ending code ] at the end of your search terms." );
35    }
36    // ensure that at least one a-z is used
37    $tmpq = str_replace(array('.','[',']'), '', $q);
38    if ( empty($tmpq) ) {
39      throw new Exception( "Your search must include at least one letter of the alphabet." );
40    }
41    // build pattern and grep command
42    $pattern = str_replace( array('[',']'), array('^','.$'), $q );
43    $cmd = $grep.' '.escapeshellarg( $pattern ).' '.$dictionary;
44    // execute and return if empty
45    $result = `$cmd`;
46    if ( empty($result) ) {
47      throw new Exception("No matching words found.");
48    }
49    // split result into words
50    $words = explode("\n",$result);
51    print "<p>".(count($words)-1)." words:</p>\n";
52    // loop through words
53    foreach( $words AS $word ) {
54      if ( empty($word) ) continue;
55      // find position of search in result
56      $matches = array();
57      preg_match( '/'.$pattern.'/', $word, $matches, PREG_OFFSET_CAPTURE );
58      //if ( $matches[0][1] ) exit('<pre>'.print_r($matches,1));
59      $matchstart = $matches[0][1];
60      $matchend = $matchstart + strlen( $matches[0][0] );
61      print "<div class='result'>";
62      $matchclass = NULL;
63      for ( $i=0; $i<strlen($word); $i++ ) {
64        if ( $i==$matchstart ) {
65          $matchclass = ' match';
66        }
67        elseif( $i==$matchend ) {
68          $matchclass = NULL;
69        }
70        $hletter = h( $word[ $i ] );
71        print "<span class='letter $hletter$matchclass'>$hletter</span>";
72      }
73      print "</div>\n";
74    }
75  } catch ( Exception $e ) {
76    ?>
77    <p><em style="color: #ee8800;"><?=h($e->getMessage())?></em></p>
78    <?
79  }
80}
81?>
82</div>
Note: See TracBrowser for help on using the repository browser.