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 get only numbers from a string using regex in PHP?
- How to remove all non-numeric characters using PHP regex?
- How to get the first match when using regex in PHP?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match a word?
- How to use PHP regex to match URL path?
- How to use PHP regex to match special characters?
- How to use PHP regex to match UUID?
- How to remove non-printable characters using PHP regex?
- How to match a plus sign in PHP regex?
See more codes...