Sep
5
PHP Regex - Search for Line Ends with a Word
September 5, 2008 |
This article teaches you how to use PHP regex - regular expression to find whole words at the end of a line.
<html>
<head><title>Searching for lines beginning with a word</title></head>
<body>
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="str"
value="<?php print $_POST['str'];?>" /><br />
<input type="submit" value="Find lines" /><br /><br />
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
$str = $_POST['str'];
if ( preg_match( "/^Word\b/", $str ) )
{
print "<b>Found a match!: &amp;amp;amp;nbsp;'". $str . "'</b><br/>";
} else {
print "<b>Didn't find it: &amp;amp;amp;nbsp;'". $str . "'</b><br/>";
}
}
?>
</form>
</body>
</html>
Regular Expression Explanation:
|
\b |
is a word boundary, such as a space, tab, and so on, followed by … |
|
f |
then … |
|
o |
then another … |
|
o |
and lastly … |
|
$ |
the end of the line. |
Similar Posts
- PHP Regex - Search for Line Begins with a Word
- PHP Regular Expression - Search for Words Within Comments


































