Oct

12

I take a break from fighting evil visitors. [] is a quite useful PHP PCRE expression that helps finding similar words.


Regular Expression Explanation:

\b a word boundary, followed by …
b letter ‘b’, followed by …
[aio] one of a, i, or o, followed by …
t letter ‘t’, and finally …
\b a word boundary.


Source Code:


<html>
<head><title>Finding Similar Words</title></head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>"
method="post">
<input type="text" name="value"
value="<?php print $_POST['value'];?>" />
<br />
<input type="submit" value="Submit" />
<br /><br />
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$mystr = $_POST['value'];
if ( preg_match( "/\bb[aio]t\b/", $mystr ) ) {
echo "Yes!<br/>";
} else {
echo "Uh, no.<br/>";
}
}
?>
</form>
</body>
</html>


Similar Posts

Comments

Name (required)

Email (required)

Website

Speak your mind

Sponsors




Links