wppaste
WordPress

wp_text_diff( string $left_string, string $right_string, string|array $args = null ): string

Since
2.6.0
Source
wp-includes/pluggable.php:3282
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.

Compatibility

WordPress
since 2.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$left_stringstring
"old" (left) version of string.
$right_stringstring
"new" (right) version of string.
$argsstring|arrayoptional
Associative array of options to pass to WP_Text_Diff_Renderer_Table().Default: null
  • $titlestringdefault: empty

    Titles the diff in a manner compatible with the output.
  • $title_leftstringdefault: empty

    Change the HTML to the left of the title.
  • $title_rightstringdefault: empty

    Change the HTML to the right of the title.
  • $show_split_viewbooldefault: true

    True for split view (two columns), false for un-split view (single column).

Return value

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

Performance profile

How much work a call to wp_text_diff() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
44–117

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 121.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_text_diff() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always44–117wp_parse_args(), normalize_whitespace(), normalize_whitespace(), explode(), explode(), ->render()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12144–11713
8.512144–11713
8.412144–117133 fewer instructions than PHP 8.3
8.312447–12013
8.212447–12013
8.112447–12013
7.412447–12013

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

Used by · 1

Source code

	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$diff\n</tbody>\n";		$r .= '</table>'; 		return $r;	}

Changelog

Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.