wptexturize_primes( string $haystack, string $needle, string $prime, string $open_quote, string $close_quote ): string
- Since
- 4.3.0
- Source
wp-includes/formatting.php:320
Compatibility
- WordPress
- since 4.3.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
$haystackstring- The plain text to be searched.
$needlestring- The character to search for such as ' or ".
$primestring- The prime char to use for replacement.
$open_quotestring- The opening quote char. Opening quote replacement must be accomplished already.
$close_quotestring- The closing quote char to use for replacement.
Return value
string- The $haystack value after primes and quotes replacements.
Performance profile
How much work a call to wptexturize_primes() 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
- Light
- Scaling
- Scales with input
- Instructions
- 25–26
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 117.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wptexturize_primes()
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost wptexturize_primes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 25–26 | wp_spaces_regexp(), explode() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 117 | 25–26 | 11 | |
| 8.5 | 117 | 25–26 | 11 | |
| 8.4 | 117 | 25–26 | 11 | 36 fewer instructions than PHP 8.3 |
| 8.3 | 153 | 28–29 | 11 | |
| 8.2 | 153 | 28–29 | 11 | |
| 8.1 | 153 | 28–29 | 11 | |
| 7.4 | 153 | 28–29 | 11 |
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
- wp_spaces_regexp()Returns the regexp for common whitespace characters.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Used by · 1
- wptexturize()Replaces common plain text characters with formatted entities.
Source code
function wptexturize_primes( $haystack, $needle, $prime, $open_quote, $close_quote ) { $spaces = wp_spaces_regexp(); $flag = '<!--wp-prime-or-quote-->'; $quote_pattern = "/$needle(?=\\Z|[.,:;!?)}\\-\\]]|>|" . $spaces . ')/'; $prime_pattern = "/(?<=\\d)$needle/"; $flag_after_digit = "/(?<=\\d)$flag/"; $flag_no_digit = "/(?<!\\d)$flag/"; $sentences = explode( $open_quote, $haystack ); foreach ( $sentences as $key => &$sentence ) { if ( ! str_contains( $sentence, $needle ) ) { continue; } elseif ( 0 !== $key && 0 === substr_count( $sentence, $close_quote ) ) { $sentence = preg_replace( $quote_pattern, $flag, $sentence, -1, $count ); if ( $count > 1 ) { // This sentence appears to have multiple closing quotes. Attempt Vulcan logic. $sentence = preg_replace( $flag_no_digit, $close_quote, $sentence, -1, $count2 ); if ( 0 === $count2 ) { // Try looking for a quote followed by a period. $count2 = substr_count( $sentence, "$flag." ); if ( $count2 > 0 ) { // Assume the rightmost quote-period match is the end of quotation. $pos = strrpos( $sentence, "$flag." ); } else { /* * When all else fails, make the rightmost candidate a closing quote. * This is most likely to be problematic in the context of bug #18549. */ $pos = strrpos( $sentence, $flag ); } $sentence = substr_replace( $sentence, $close_quote, $pos, strlen( $flag ) ); } // Use conventional replacement on any remaining primes and quotes. $sentence = preg_replace( $prime_pattern, $prime, $sentence ); $sentence = preg_replace( $flag_after_digit, $prime, $sentence ); $sentence = str_replace( $flag, $close_quote, $sentence ); } elseif ( 1 === $count ) { // Found only one closing quote candidate, so give it priority over primes. $sentence = str_replace( $flag, $close_quote, $sentence ); $sentence = preg_replace( $prime_pattern, $prime, $sentence ); } else { // No closing quotes found. Just run primes pattern. $sentence = preg_replace( $prime_pattern, $prime, $sentence ); } } else { $sentence = preg_replace( $prime_pattern, $prime, $sentence ); $sentence = preg_replace( $quote_pattern, $close_quote, $sentence ); } if ( '"' === $needle && str_contains( $sentence, '"' ) ) { $sentence = str_replace( '"', $close_quote, $sentence ); } } return implode( $open_quote, $sentences );}Changelog
Introduced in 4.3.0. Unchanged from 6.7.7 through 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.