Sep
4
PHP Regular Expression - Search for Words Within Comments
September 4, 2008 |
You can use this PHP regular expression to search for a word in code and to make sure the word is found within comments. The string // WORD will match on a line and so will /* WORD */.
<html>
<head><title>Searching for words within comments</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 WORD in comments" /><br /><br />
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
$str = $_POST['str'];
if ( preg_match( "/^(?:\/\*(?:(?!\*\/).)*|\/\/.*?)WORD/", $str ) )
{
print "<b>Found WORD in comments: '" . htmlspecialchars($str)
.."'</b><br/>";
}
else
{
print "<b>Found no match in text: '" .
htmlspecialchars($str) . "'</b><br/>";
}
}
?>
</form>
</body>
</html>
Similar Posts
Comments
2 Comments so far



































This is a very useful article, i think that’s how wordpress reads plugin info.
Привет, я думала что это совсем не так происходит:)
——————————————-
Мой блог: http://medved-vyshivka.blog.ru/