wppaste
WordPress

sanitize_title_with_dashes( string $title, string $raw_title = '', string $context = 'display' ): string

Since
1.2.0
Source
wp-includes/formatting.php:2261
Sanitizes a title, replacing whitespace and a few other characters with dashes.

Description

Limits the output to alphanumeric characters, underscore (_) and dash (-).
Whitespace becomes a dash.

Compatibility

WordPress
since 1.2.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

$titlestring
The title to be sanitized.
$raw_titlestringoptional
Not used. Default empty.Default: ''
$contextstringoptional
The operation for which the string is sanitized.
When set to 'save', additional entities are converted to hyphens or stripped entirely. Default 'display'.Default: 'display'

Return value

string
The sanitized title.

Performance profile

How much work a call to sanitize_title_with_dashes() 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
44–72

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_title_with_dashes() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!seems_utf8()44–62strip_tags(), seems_utf8(), strtolower()
seems_utf8()54–72strip_tags(), seems_utf8(), mb_strtolower(), utf8_uri_encode(), strtolower()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7644–7634 more instructions than PHP 8.5
8.57244–722
8.47244–72245 fewer instructions than PHP 8.3
8.311771–1172
8.211771–1172
8.111771–1172
7.411771–1172

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 · 2

Used by · 8

Source code

function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) {	$title = strip_tags( $title );	// Preserve escaped octets.	$title = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title );	// Remove percent signs that are not part of an octet.	$title = str_replace( '%', '', $title );	// Restore octets.	$title = preg_replace( '|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title ); 	if ( seems_utf8( $title ) ) {		if ( function_exists( 'mb_strtolower' ) ) {			$title = mb_strtolower( $title, 'UTF-8' );		}		$title = utf8_uri_encode( $title, 200 );	} 	$title = strtolower( $title ); 	if ( 'save' === $context ) {		// Convert &nbsp, &ndash, and &mdash to hyphens.		$title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );		// Convert &nbsp, &ndash, and &mdash HTML entities to hyphens.		$title = str_replace( array( ' ', ' ', '–', '–', '—', '—' ), '-', $title );		// Convert forward slash to hyphen.		$title = str_replace( '/', '-', $title ); 		// Strip these characters entirely.		$title = str_replace(			array(				// Soft hyphens.				'%c2%ad',				// &iexcl and &iquest.				'%c2%a1',				'%c2%bf',				// Angle quotes.				'%c2%ab',				'%c2%bb',				'%e2%80%b9',				'%e2%80%ba',				// Curly quotes.				'%e2%80%98',				'%e2%80%99',				'%e2%80%9c',				'%e2%80%9d',				'%e2%80%9a',				'%e2%80%9b',				'%e2%80%9e',				'%e2%80%9f',				// Bullet.				'%e2%80%a2',				// &copy, &reg, &deg, &hellip, and &trade.				'%c2%a9',				'%c2%ae',				'%c2%b0',				'%e2%80%a6',				'%e2%84%a2',				// Acute accents.				'%c2%b4',				'%cb%8a',				'%cc%81',				'%cd%81',				// Grave accent, macron, caron.				'%cc%80',				'%cc%84',				'%cc%8c',				// Non-visible characters that display without a width.				'%e2%80%8b', // Zero width space.				'%e2%80%8c', // Zero width non-joiner.				'%e2%80%8d', // Zero width joiner.				'%e2%80%8e', // Left-to-right mark.				'%e2%80%8f', // Right-to-left mark.				'%e2%80%aa', // Left-to-right embedding.				'%e2%80%ab', // Right-to-left embedding.				'%e2%80%ac', // Pop directional formatting.				'%e2%80%ad', // Left-to-right override.				'%e2%80%ae', // Right-to-left override.				'%ef%bb%bf', // Byte order mark.				'%ef%bf%bc', // Object replacement character.			),			'',

Changelog

Introduced in 1.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/formatting.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.