From f066f5ed430ffc626f73c48fffac50903ab04c22 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 30 Nov 2006 20:26:42 +0000 Subject: [PATCH] Truncate sanitized titles to the size of the post_name field without killing multibye characters. git-svn-id: http://svn.automattic.com/wordpress/trunk@4560 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 2720f2387..ea0f8a109 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -116,7 +116,7 @@ function wp_specialchars( $text, $quotes = 0 ) { return $text; } -function utf8_uri_encode( $utf8_string ) { +function utf8_uri_encode( $utf8_string, $length = 0 ) { $unicode = ''; $values = array(); $num_octets = 1; @@ -126,21 +126,25 @@ function utf8_uri_encode( $utf8_string ) { $value = ord( $utf8_string[ $i ] ); if ( $value < 128 ) { + if ( $length && ( strlen($unicode) + 1 > $length ) ) + break; $unicode .= chr($value); } else { if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3; $values[] = $value; + if ( $length && ( (strlen($unicode) + ($num_octets * 3)) > $length ) ) + break; if ( count( $values ) == $num_octets ) { - if ($num_octets == 3) { - $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]); - } else { - $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]); - } + if ($num_octets == 3) { + $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]); + } else { + $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]); + } - $values = array(); - $num_octets = 1; + $values = array(); + $num_octets = 1; } } } @@ -317,7 +321,7 @@ function sanitize_title_with_dashes($title) { if (function_exists('mb_strtolower')) { $title = mb_strtolower($title, 'UTF-8'); } - $title = utf8_uri_encode($title); + $title = utf8_uri_encode($title, 200); } $title = strtolower($title);