php-regexHow to use PHP regex to match a hyphen?
To match a hyphen using PHP regex, you can use the -
character. For example:
$string = 'This-is-a-string';
if (preg_match('/-/', $string)) {
echo 'Match found';
}
Output example
Match found
The code above uses the preg_match()
function to check if the -
character is present in the string. If it is, it prints out Match found
.
Code explanation
$string = 'This-is-a-string';
: This is a string containing a hyphen.preg_match('/-/', $string)
: This is the regex expression used to match the hyphen. The-
character is used to match the hyphen.echo 'Match found';
: This is the output printed when a match is found.
Helpful links
More of Php Regex
- How to use PHP regex to match an exact string?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to use PHP regex to get a YouTube video ID?
- How to match a space using PHP regex?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match a year?
- How to use PHP regex to match UUID?
- How to use the "s" modifier in PHP regex?
See more codes...