devonc0 Posted November 7, 2010 Share Posted November 7, 2010 <html><title>program6</title> <body> <form method="POST" action=" <?php echo $PHP_SELF;?> "> <?php echo "<select name=s>"; for ($count=65; $count<=90; $count++) { echo "<option value=".chr($count).">".chr($count)."</option><br>"; } echo "</select>"; ?> <?php echo "<select name=t>"; for ($count=65; $count<=90; $count++) { echo "<option value=".chr($count).">".chr($count)."</option><br>"; } echo "</select>"; ?> <input type="submit"/> </form> <hr> <?php if (isset($s)) { $first=strtolower($_POST["s"]); $second=strtolower($_POST["t"]); echo "A word that starts with ".$first." and ends with ".$second." is: <br>"; preg_match("/^$first.*%second/i", file('/words'), $words); echo $words[1]; } ?> </body> </html> Ok, so what this program is doing is taking 2 letters (chosen by user from SELECT options) and then calling the php script to search through the 'word' file in the local directory for 3 words that begin with the first option and end with the second: Basically: if user chooses A and T... OUTPUT: Adult, Agincourt, Ant For some reason, I can't wrap my mind around why I can't get this to work, I'm assuming it's the regular expression or something, btw, this is my first day/project using PHP, so PLEASE explain this as if I were mildly slow Quote Link to comment https://forums.phpfreaks.com/topic/218050-possibly-easy-mistake-preg_match-and-wordlist/ Share on other sites More sharing options...
sasa Posted November 8, 2010 Share Posted November 8, 2010 try <?php $words = 'sasa blah, foo. bar.'; $first = 'b'; $second = 'r'; preg_match_all('/\b'.$first.'[a-z]*'.$second.'\b/i', $words, $matches); print_r($matches[0]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/218050-possibly-easy-mistake-preg_match-and-wordlist/#findComment-1131648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.