Title: WP_Text_Diff_Renderer_Table::compute_string_distance
Published: April 25, 2014
Last modified: August 20, 2026

---

# WP_Text_Diff_Renderer_Table::compute_string_distance( string $string1, string $string2 ): int

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#changelog)

Computes a number that is intended to reflect the “distance” between two strings.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#parameters)󠁿

 `$string1`stringrequired

`$string2`stringrequired

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#return)󠁿

 int

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#source)󠁿

    ```php
    public function compute_string_distance( $string1, $string2 ) {
    	// Use an md5 hash of the strings for a count cache, as it's fast to generate, and collisions aren't a concern.
    	$count_key1 = md5( $string1 );
    	$count_key2 = md5( $string2 );

    	// Cache vectors containing character frequency for all chars in each string.
    	if ( ! isset( $this->count_cache[ $count_key1 ] ) ) {
    		$this->count_cache[ $count_key1 ] = count_chars( $string1 );
    	}
    	if ( ! isset( $this->count_cache[ $count_key2 ] ) ) {
    		$this->count_cache[ $count_key2 ] = count_chars( $string2 );
    	}

    	$chars1 = $this->count_cache[ $count_key1 ];
    	$chars2 = $this->count_cache[ $count_key2 ];

    	$difference_key = md5( implode( ',', $chars1 ) . ':' . implode( ',', $chars2 ) );
    	if ( ! isset( $this->difference_cache[ $difference_key ] ) ) {
    		// L1-norm of difference vector.
    		$this->difference_cache[ $difference_key ] = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) );
    	}

    	$difference = $this->difference_cache[ $difference_key ];

    	// $string1 has zero length? Odd. Give huge penalty by not dividing.
    	if ( ! $string1 ) {
    		return $difference;
    	}

    	// Return distance per character (of string1).
    	return $difference / strlen( $string1 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-text-diff-renderer-table.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-text-diff-renderer-table.php#L493)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-text-diff-renderer-table.php#L493-L524)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Text_Diff_Renderer_Table::interleave_changed_lines()](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/interleave_changed_lines/)`wp-includes/class-wp-text-diff-renderer-table.php` |

Takes changed blocks and matches which rows in orig turned into which rows in final.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_text_diff_renderer_table/compute_string_distance/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.6.0](https://developer.wordpress.org/reference/since/2.6.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_text_diff_renderer_table%2Fcompute_string_distance%2F)
before being able to contribute a note or feedback.