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 get last matched occurrence in PHP regex?
- How to use PHP regex to match a zip code?
- How to convert a PHP regex to JavaScript regex?
- How to use PHP regex to match special characters?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match an XML tag?
- How to get the first match when using regex in PHP?
- How to use PHP regex to match a year?
- How to match a space using PHP regex?
See more codes...