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 match a single quote in PHP regex?
- How to use PHP regex to match a zip code?
- How to use PHP regex to match an exact string?
- How to match a double quote in PHP regex?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match UUID?
- How to use regex in PHP to validate an email address?
- How to remove a tag from a string using PHP regex?
See more codes...