php-regexHow to use PHP regex to match a nbsp HTML whitespace?
PHP regex can be used to match a nbsp HTML whitespace by using the \s
character class. This character class matches any whitespace character, including the nbsp HTML whitespace.
$string = 'This is a string with a nbsp whitespace.';
if (preg_match_all('/(\s+| +)/', $string)) {
echo 'Matched whitespace!';
}
Output example
Matched whitespace!
Code explanation
\s+| +
: character class that matches any whitespace character, including the nbsp HTML whitespacepreg_match()
: PHP function that searches a string for a pattern, and returns true if the pattern exists, and false otherwise
Helpful links
More of Php Regex
- How to use PHP regex with the "x" modifier?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match special characters?
- How to use PHP regex to match a zip code?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match a year?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match whitespace?
See more codes...