PHP Find Words

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

You can use this recipe for finding single words in a block of text. The expression will find only complete words surrounded by spaces.

[edit] code

<html>
<head><title>1-2 Finding words</title></head>
<body>
<form action="recipe1-2.php" method="post">
<input type="text" name="str"
        value="<?php print $_POST['str'];?>" /><br />
<input type="submit" value="Find word" /><br /><br />
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
    $str = $_POST['str'];
    if ( preg_match( "/\bword\b/", $str ) )
    {
        print "<b>Heh heh. You said 'word'</b>";
    }
    else
    {
        print "<b>Nope. Didn't find it.</b>";
    }
}
?>
</form>
</body>
</html>

[edit] How It Works

A special character class in Perl, \b, allows you to easily search for whole words. This is an advantage because without doing a whole bunch of extra work you can make sure that a search for word, for example, doesn't yield unexpected matches such as sword.

You can easily break the regular expression shown here into the following:

Regular Expression Description
\b a word boundary (a space or beginning of a line, or punctuation) . . .
w a w followed by . . .
o an o, followed by . . .
r an r, then . . .
d a d, and finally . . .
\b a word boundary at the end of the word . . .
Personal tools