函数文档

wp_text_diff()

💡 云策文档标注

概述

wp_text_diff() 函数用于生成两个字符串之间差异的可读 HTML 表示,主要用于版本比较和显示变更。如果字符串相同,则返回空字符串。

关键要点

  • 核心功能:比较两个字符串并输出 HTML 格式的差异表,适用于版本控制场景。
  • 参数说明:$left_string 和 $right_string 为必需参数,分别代表旧版本和新版本字符串;$args 为可选参数,用于传递配置选项给 WP_Text_Diff_Renderer_Table()。
  • 返回值:字符串相同返回空字符串,否则返回包含差异的 HTML。
  • 依赖关系:内部使用 WP_Text_Diff_Renderer_Table 类,需确保 wp-diff.php 已加载。
  • 相关函数:与 wp_parse_args() 和 normalize_whitespace() 等函数关联,用于参数处理和字符串标准化。

代码示例

$left_string = 'This is the original string';
$right_string = 'This is the revised string';
$args = array(
    'title'       => 'Differences',
    'title_left'  => 'Old Version',
    'title_right' => 'New Version'
);
$diff_table = wp_text_diff( $left_string, $right_string, $args );
echo $diff_table;

注意事项

  • 函数内部自动调用 normalize_whitespace() 标准化字符串,确保比较准确性。
  • 默认启用分列视图(show_split_view 为 true),可通过 $args 调整。
  • 需注意类 WP_Text_Diff_Renderer_Table 的可用性,必要时会动态加载 wp-diff.php。

📄 原文内容

Displays a human readable HTML representation of the difference between two strings.

Description

The Diff is available for getting the changes between versions. The output is HTML, so the primary use is for displaying the changes. If the two strings are equivalent, then an empty string will be returned.

See also

Parameters

$left_stringstringrequired
“old” (left) version of string.
$right_stringstringrequired
“new” (right) version of string.
$argsstring|arrayoptional
Associative array of options to pass to WP_Text_Diff_Renderer_Table().

  • title string
    Titles the diff in a manner compatible with the output.
  • title_left string
    Change the HTML to the left of the title.
  • title_right string
    Change the HTML to the right of the title.
  • show_split_view bool
    True for split view (two columns), false for un-split view (single column). Default true.

Default:null

Return

string Empty string if strings are equivalent or HTML with differences.

Source

function wp_text_diff( $left_string, $right_string, $args = null ) {
	$defaults = array(
		'title'           => '',
		'title_left'      => '',
		'title_right'     => '',
		'show_split_view' => true,
	);
	$args     = wp_parse_args( $args, $defaults );

	if ( ! class_exists( 'WP_Text_Diff_Renderer_Table', false ) ) {
		require ABSPATH . WPINC . '/wp-diff.php';
	}

	$left_string  = normalize_whitespace( $left_string );
	$right_string = normalize_whitespace( $right_string );

	$left_lines  = explode( "n", $left_string );
	$right_lines = explode( "n", $right_string );
	$text_diff   = new Text_Diff( $left_lines, $right_lines );
	$renderer    = new WP_Text_Diff_Renderer_Table( $args );
	$diff        = $renderer->render( $text_diff );

	if ( ! $diff ) {
		return '';
	}

	$is_split_view       = ! empty( $args['show_split_view'] );
	$is_split_view_class = $is_split_view ? ' is-split-view' : '';

	$r = "<table class='diff$is_split_view_class'>n";

	if ( $args['title'] ) {
		$r .= "<caption class='diff-title'>$args[title]</caption>n";
	}

	if ( $args['title_left'] || $args['title_right'] ) {
		$r .= '<thead>';
	}

	if ( $args['title_left'] || $args['title_right'] ) {
		$th_or_td_left  = empty( $args['title_left'] ) ? 'td' : 'th';
		$th_or_td_right = empty( $args['title_right'] ) ? 'td' : 'th';

		$r .= "<tr class='diff-sub-title'>n";
		$r .= "t<$th_or_td_left>$args[title_left]<!--$th_or_td_left-->n";
		if ( $is_split_view ) {
			$r .= "t<$th_or_td_right>$args[title_right]<!--$th_or_td_right-->n";
		}
		$r .= "</tr>n";
	}

	if ( $args['title_left'] || $args['title_right'] ) {
		$r .= "</thead>n";
	}

	$r .= "<tbody>n$diffn</tbody>n";
	$r .= '</table>';

	return $r;
}

Changelog

Version Description
2.6.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example

    $left_string = 'This is the original string';
    
    $right_string = 'This is the revised string';
    
    $args = array(
    	'title'       => 'Differences',
    	'title_left'  => 'Old Version',
    	'title_right' => 'New Version'
    );
    
    $diff_table = wp_text_diff( $left_string,$right_string, $args );
    
    echo $diff_table;

    This will output the following html:

    [html]

    Differences
    Old Version New version
    This is the original string + This is the revised string

    [/html]