wp_text_diff( string $left_string, string $right_string, string|array $args = null ): string
- Since
- 2.6.0
- Source
wp-includes/pluggable.php:3391
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: emptyTitles the diff in a manner compatible with the output.$title_leftstringdefault: emptyChange the HTML to the left of the title.$title_rightstringdefault: emptyChange the HTML to the right of the title.$show_split_viewbooldefault: trueTrue 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
- Scaling
- Constant
- Instructions
- 44–117
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 121.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 44–117 | wp_parse_args(), normalize_whitespace(), normalize_whitespace(), explode(), explode(), ->render() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 121 | 44–117 | 13 | |
| 8.5 | 121 | 44–117 | 13 | |
| 8.4 | 121 | 44–117 | 13 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 124 | 47–120 | 13 | |
| 8.2 | 124 | 47–120 | 13 | |
| 8.1 | 124 | 47–120 | 13 | |
| 7.4 | 124 | 47–120 | 13 |
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
- wp_parse_args()Merges user defined arguments into defaults array.
- normalize_whitespace()Normalizes EOL characters and strips duplicate whitespace.
- Text_Diff::__construct()Computes diffs between sequences of strings.
- WP_Text_Diff_Renderer_Table::__construct()Constructor - Call parent constructor with params array.
Used by · 1
- wp_get_revision_ui_diff()Get the revision UI diff.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.