wp_spaces_regexp()
Returns a regular expression that matches all kinds of spaces (line breaks, invisible characters, etc.).
By spaces in this case, we mean: line break, tab, the entity and non-breaking space in utf-8 encoding - \xC2\xA0.
1 time — 0.000001 sec (speed of light) | 50000 times — 0.000001 sec (speed of light) | PHP 7.3.12, WP 5.3.2
Hooks from the function
Returns
String. Regular expression: [\r\n\t ]|\xC2\xA0| .
Usage
wp_spaces_regexp();
Examples
#1 Example of using the function
An example of a regular expression using the wp_spaces_regexp() function.
$spaces = wp_spaces_regexp(); // string(25) "[\r\n\t ]|\xC2\xA0| "
var_dump($spaces);
$text = " \t footer";
$pattern = "(?:{$spaces})*foo"; // possible starting spaces
echo preg_replace( "/$pattern/", '', $text );
// outputs "ter"
Changelog
| Since 4.0.0 | Introduced. |
wp_spaces_regexp() wp spaces regexp code WP 7.0
function wp_spaces_regexp() {
static $spaces = '';
if ( empty( $spaces ) ) {
/**
* Filters the regexp for common whitespace characters.
*
* This string is substituted for the \s sequence as needed in regular
* expressions. For websites not written in English, different characters
* may represent whitespace. For websites not encoded in UTF-8, the 0xC2 0xA0
* sequence may not be in use.
*
* @since 4.0.0
*
* @param string $spaces Regexp pattern for matching common whitespace characters.
*/
$spaces = apply_filters( 'wp_spaces_regexp', '[\r\n\t ]|\xC2\xA0| ' );
}
return $spaces;
}