类文档

WP_HTML_Text_Replacement

💡 云策文档标注

概述

WP_HTML_Text_Replacement 是 WP_HTML_Tag_Processor 内部使用的核心类,用于高效替换 HTML 文档中的文本内容。它通过指定起始位置和长度来定义替换范围,从而显著提升性能。

关键要点

  • 该类是 WP_HTML_Tag_Processor 的内部数据结构,用于从起始到结束替换现有内容。
  • 包含三个公共属性:$start(起始字节偏移)、$length(替换长度)和 $text(要插入的文本)。
  • 构造函数 __construct 接受 $start、$length 和 $text 参数来初始化对象。
  • 自 WordPress 6.2.0 引入,6.5.0 版本将 $end 改为 $length 以更匹配 substr() 函数。

代码示例

class WP_HTML_Text_Replacement {
    public $start;
    public $length;
    public $text;

    public function __construct( int $start, int $length, string $text ) {
        $this->start  = $start;
        $this->length = $length;
        $this->text   = $text;
    }
}

📄 原文内容

Core class used by the HTML tag processor as a data structure for replacing existing content from start to end, allowing to drastically improve performance.

Description

This class is for internal usage of the WP_HTML_Tag_Processor class.

See also

Methods

Name Description
WP_HTML_Text_Replacement::__construct Constructor.

Source

class WP_HTML_Text_Replacement {
	/**
	 * Byte offset into document where replacement span begins.
	 *
	 * @since 6.2.0
	 *
	 * @var int
	 */
	public $start;

	/**
	 * Byte length of span being replaced.
	 *
	 * @since 6.5.0
	 *
	 * @var int
	 */
	public $length;

	/**
	 * Span of text to insert in document to replace existing content from start to end.
	 *
	 * @since 6.2.0
	 *
	 * @var string
	 */
	public $text;

	/**
	 * Constructor.
	 *
	 * @since 6.2.0
	 *
	 * @param int    $start  Byte offset into document where replacement span begins.
	 * @param int    $length Byte length of span in document being replaced.
	 * @param string $text   Span of text to insert in document to replace existing content from start to end.
	 */
	public function __construct( int $start, int $length, string $text ) {
		$this->start  = $start;
		$this->length = $length;
		$this->text   = $text;
	}
}

Changelog

Version Description
6.5.0 Replace end with length to more closely match substr().
6.2.0 Introduced.