Skip to content

Instantly share code, notes, and snippets.

@GabeStah
Created December 9, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GabeStah/33350b067e4ec8954e49 to your computer and use it in GitHub Desktop.
Save GabeStah/33350b067e4ec8954e49 to your computer and use it in GitHub Desktop.
Coding Dojo - PHP Best Practices - Using Regular Expressions
<?php
$searchTerm = "coding";
/*
* Using the \b flag, we're searching only for whole words.
*/
if (preg_match("/\b$searchTerm\b/i", "Coding Dojo is the best!")) {
echo "Match found.<br/>";
} else {
echo "No match found.<br/>";
}
/*
* Since our search term "coding" is only part of a larger word (decoding), no match will be found.
*/
if (preg_match("/\b$searchTerm\b/i", "Decoding encryptions is just one of my many sexy hobbies.")) {
echo "Match found.<br/>";
} else {
echo "No match found.<br/>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment