php-regexHow to use PHP regex to match a word?
Using PHP regex to match a word is a powerful way to search for specific words or patterns in a string.
$string = "This is a string";
if (preg_match("/string/", $string)) {
echo "Match found!";
}
Output example
Match found!
The code above uses the preg_match()
function to search for the word "string" in the string $string
. If the word is found, the echo
statement will output "Match found!".
Code explanation
preg_match()
: This is a PHP function used to search for a specific pattern in a string./string/
: This is the pattern we are searching for. It is enclosed in forward slashes.$string
: This is the string we are searching in.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to match a space using PHP regex?
- How to use PHP regex to match a zip code?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match an exact string?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match URL path?
- How to use named capture groups in PHP regex?
- How to use PHP regex to match special characters?
See more codes...