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 use PHP regex to match special characters?
- How to use an "or" condition in PHP regex?
- How to use regex in PHP to match dot character?
- 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 URL path?
- How to use PHP regex to match UTF8?
- How to use PHP regex to match tab?
- How to match strings starting with a certain string using PHP regex?
See more codes...