wp_spaces_regexp()
云策文档标注
概述
wp_spaces_regexp() 函数返回一个正则表达式模式,用于匹配常见的空白字符,包括换行符、制表符、空格和 UTF-8 非换行空格。它旨在替代 PCRE 的 s 序列,以解决在某些情况下不可靠的问题。
关键要点
- 函数返回一个字符串,表示空白字符的正则表达式模式。
- 默认模式包括 [rnt ]|xC2xA0| ,覆盖换行符、制表符、空格和 UTF-8 非换行空格。
- 通过 wp_spaces_regexp 过滤器,允许开发者自定义空白字符的正则表达式,以适应非英语网站或非 UTF-8 编码的需求。
- 函数使用静态变量缓存正则表达式,以提高性能。
代码示例
function wp_spaces_regexp() {
static $spaces = '';
if ( empty( $spaces ) ) {
$spaces = apply_filters( 'wp_spaces_regexp', '[rnt ]|xC2xA0| ' );
}
return $spaces;
}注意事项
- 此函数从 WordPress 4.0.0 版本引入,用于解决 PCRE s 序列在特定场景下不可靠的问题(如 ticket #22692)。
- 在非英语网站或非 UTF-8 编码环境中,可能需要通过过滤器调整正则表达式以匹配本地化的空白字符。
原文内容
Returns the regexp for common whitespace characters.
Description
By default, spaces include new lines, tabs, nbsp entities, and the UTF-8 nbsp.
This is designed to replace the PCRE s sequence. In ticket #22692, that sequence was found to be unreliable due to random inclusion of the A0 byte.
Source
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', '[rnt ]|xC2xA0| ' );
}
return $spaces;
}
Hooks
- apply_filters( ‘wp_spaces_regexp’, string $spaces )
-
Filters the regexp for common whitespace characters.
Changelog
| Version | Description |
|---|---|
| 4.0.0 | Introduced. |