Skip to content

Instantly share code, notes, and snippets.

@GabeStah
Created Dec 9, 2015
Embed
What would you like to do?
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