函数文档

wp_kses_no_null()

💡 云策文档标注

概述

wp_kses_no_null() 函数用于从文本字符串中移除无效的控制字符,包括空字符()。它提供选项来控制是否保留反斜杠零序列。

关键要点

  • 函数移除 ASCII 控制字符(范围 x00-x08, x0B, x0C, x0E-x1F)。
  • 可选参数 $options 可设置 'slash_zero' 为 'keep' 以允许反斜杠零序列,默认值为 'remove'。
  • 返回过滤后的字符串,常用于 HTML 和 URL 安全处理。

代码示例

function wp_kses_no_null( $content, $options = null ) {
    if ( ! isset( $options['slash_zero'] ) ) {
        $options = array( 'slash_zero' => 'remove' );
    }

    $content = preg_replace( '/[x00-x08x0Bx0Cx0E-x1F]/', '', $content );
    if ( 'remove' === $options['slash_zero'] ) {
        $content = preg_replace( '/\\+0+/', '', $content );
    }

    return $content;
}

注意事项

  • 函数自 WordPress 1.0.0 版本引入,是 wp_kses() 相关函数的一部分。
  • 常用于 wp_kses_one_attr()、wp_sanitize_redirect() 等函数中,确保数据安全。

📄 原文内容

Removes any invalid control characters in a text string.

Description

Also removes any instance of the string.

Parameters

$contentstringrequired
Content to filter null characters from.
$optionsarrayoptional
Set 'slash_zero' => 'keep' when '' is allowed. Default is 'remove'.

Default:null

Return

string Filtered content.

Source

function wp_kses_no_null( $content, $options = null ) {
	if ( ! isset( $options['slash_zero'] ) ) {
		$options = array( 'slash_zero' => 'remove' );
	}

	$content = preg_replace( '/[x00-x08x0Bx0Cx0E-x1F]/', '', $content );
	if ( 'remove' === $options['slash_zero'] ) {
		$content = preg_replace( '/\\+0+/', '', $content );
	}

	return $content;
}

Changelog

Version Description
1.0.0 Introduced.