Skip to content

Commit ff2d2a7

Browse files
committed
Refactor replacement of dashes to hyphens
1 parent 7c2fa71 commit ff2d2a7

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/wp-includes/formatting.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,12 +2294,42 @@ function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'displa
22942294
$title = strtolower( $title );
22952295

22962296
if ( 'save' === $context ) {
2297-
// Convert &nbsp, non-breaking hyphen, &ndash, and &mdash to hyphens.
2298-
$title = str_replace( array( '%c2%a0', '%e2%80%91', '%e2%80%93', '%e2%80%94' ), '-', $title );
2299-
// Convert &nbsp, non-breaking hyphen, &ndash, and &mdash HTML entities to hyphens.
2300-
$title = str_replace( array( ' ', '‑', ' ', '–', '–', '—', '—' ), '-', $title );
2297+
// Mapping of Unicode hex codepoint to HTML named entity (if it exists).
2298+
$dash_characters = array(
2299+
// Non-breaking space (U+00A0).
2300+
'A0' => 'nbsp',
2301+
2302+
// Non-breaking hyphen (U+2011).
2303+
'2011' => null,
2304+
2305+
// En dash (U+2013).
2306+
'2013' => 'ndash',
2307+
2308+
// Em dash (U+2014).
2309+
'2014' => 'mdash',
2310+
);
2311+
2312+
// Convert dashes to hyphens.
2313+
$replacements = array();
2314+
foreach ( $dash_characters as $hex_codepoint => $named_entity ) {
2315+
// HTML entities.
2316+
$replacements[] = '&#x' . $hex_codepoint . ';';
2317+
if ( $named_entity ) {
2318+
$replacements[] = '&' . $named_entity . ';';
2319+
}
2320+
$decimal_codepoint = hexdec( $hex_codepoint );
2321+
$replacements[] = '&#' . $decimal_codepoint . ';';
2322+
2323+
// URL-encoded characters.
2324+
if ( function_exists( 'mb_chr' ) ) {
2325+
$replacements[] = rawurlencode( mb_chr( $decimal_codepoint, 'UTF-8' ) );
2326+
}
2327+
}
2328+
23012329
// Convert forward slash to hyphen.
2302-
$title = str_replace( '/', '-', $title );
2330+
$replacements[] = '/';
2331+
2332+
$title = str_ireplace( $replacements, '-', $title );
23032333

23042334
// Strip these characters entirely.
23052335
$title = str_replace(

0 commit comments

Comments
 (0)