-
-
Save GabeStah/33350b067e4ec8954e49 to your computer and use it in GitHub Desktop.
Coding Dojo - PHP Best Practices - Using Regular Expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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