WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp text diff

Description

Displays a human readable HTML table with the difference between two strings.

If the two strings are equivalent, wp_text_diff returns an empty string. Otherwise, returns an html table showing the changes. Deletions and insertions in text are marked up using the <del> and <ins> html tags, respectively.

Primary use is for for displaying changes between an older version and a new revision of the string.

@uses Text_Diff @uses WP_Text_Diff_Renderer_Table

Usage

<?php wp_text_diff$left_string$right_string$args ); ?>

Parameters

$left_string
(string) (required) "old" (left) version of string.
Default: None
$right_string
(string) (required) "new" (right) version of string.
Default: None
$args
(string) (Optional. Change 'title', 'title_left', and 'title_right' defaults.) array
Default: optional

Arguments

title
(string) (optional) Optional. Default is an empty string. Sets the title for the diff table.
Default: null
title_left
(string) (optional) Optional. Default is an empty string. Sets the title for the left column (old version) of the diff table.
Default: null
title_right
(string) (optional) Optional. Default is an empty string. Sets the title for the right column (new version) of the diff table.
Default: null

Return Values

(string) 
Empty string if strings are equivalent or an HTML table of differences.

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:

<table class="diff">
	<colgroup>
		<col class="ltype">
		<col class="content">
		<col class="ltype">
		<col class="content">
	</colgroup>
	<thead>
		<tr class="diff-title">
			<th colspan="4">Differences</th>
		</tr>
		<tr class="diff-sub-title">
			<td></td>
			<th>Old Version</th>
			<td></td>
			<th>New version</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>-</td>
			<td class="diff-deletedline">
				This is the <del>original</del> string
			</td>
			<td>+</td>
			<td class="diff-addedline">
				This is the <ins>revised</ins> string
			</td>
		</tr>

	</tbody>
</table>

Sample output:

Differences
Old Version New version
-

This is the original string

+

This is the revised string

Change Log

Since: 2.6

Source File

wp_text_diff() is located in wp-includes/pluggable.php