wptexturize_primes

Implements a logic tree to determine whether or not "7'." represents seven feet, then converts the special char into either a prime char or a closing quote char.

Description

(string) wptexturize_primes( (string) $haystack, (string) $needle, (string) $prime, (string) $open_quote, (string) $close_quote ); 

Returns (string)

The $haystack value after primes and quotes replacements.

Parameters (5)

0. $haystack (string)
The plain text to be searched.
1. $needle (string)
The character to search for such as or ..
2. $prime (string)
The prime char to use for replacement.
3. $open_quote (string)
The opening quote char. Opening quote replacement must be accomplished already.
4. $close_quote (string)
The closing quote char to use for replacement.

Usage

  1. if ( !function_exists( 'wptexturize_primes' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The plain text to be searched. 
  5. $haystack = ''; 
  6.  
  7. // The character to search for such as ' or ". 
  8. $needle = ''; 
  9.  
  10. // The prime char to use for replacement. 
  11. $prime = ''; 
  12.  
  13. // The opening quote char. Opening quote replacement must be 
  14. // accomplished already. 
  15. $open_quote = ''; 
  16.  
  17. // The closing quote char to use for replacement. 
  18. $close_quote = ''; 
  19.  
  20. // NOTICE! Understand what this does before running. 
  21. $result = wptexturize_primes($haystack, $needle, $prime, $open_quote, $close_quote); 
  22.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function wptexturize_primes( $haystack, $needle, $prime, $open_quote, $close_quote ) { 
  2. $spaces = wp_spaces_regexp(); 
  3. $flag = '<!--wp-prime-or-quote-->'; 
  4. $quote_pattern = "/$needle(?=\\Z|[., :;!?)}\\-\\]]|>|" . $spaces . ")/"; 
  5. $prime_pattern = "/(?<=\\d)$needle/"; 
  6. $flag_after_digit = "/(?<=\\d)$flag/"; 
  7. $flag_no_digit = "/(?<!\\d)$flag/"; 
  8.  
  9. $sentences = explode( $open_quote, $haystack ); 
  10.  
  11. foreach ( $sentences as $key => &$sentence ) { 
  12. if ( false === strpos( $sentence, $needle ) ) { 
  13. continue; 
  14. } elseif ( 0 !== $key && 0 === substr_count( $sentence, $close_quote ) ) { 
  15. $sentence = preg_replace( $quote_pattern, $flag, $sentence, -1, $count ); 
  16. if ( $count > 1 ) { 
  17. // This sentence appears to have multiple closing quotes. Attempt Vulcan logic. 
  18. $sentence = preg_replace( $flag_no_digit, $close_quote, $sentence, -1, $count2 ); 
  19. if ( 0 === $count2 ) { 
  20. // Try looking for a quote followed by a period. 
  21. $count2 = substr_count( $sentence, "$flag." ); 
  22. if ( $count2 > 0 ) { 
  23. // Assume the rightmost quote-period match is the end of quotation. 
  24. $pos = strrpos( $sentence, "$flag." ); 
  25. } else { 
  26. // When all else fails, make the rightmost candidate a closing quote. 
  27. // This is most likely to be problematic in the context of bug #18549. 
  28. $pos = strrpos( $sentence, $flag ); 
  29. $sentence = substr_replace( $sentence, $close_quote, $pos, strlen( $flag ) ); 
  30. // Use conventional replacement on any remaining primes and quotes. 
  31. $sentence = preg_replace( $prime_pattern, $prime, $sentence ); 
  32. $sentence = preg_replace( $flag_after_digit, $prime, $sentence ); 
  33. $sentence = str_replace( $flag, $close_quote, $sentence ); 
  34. } elseif ( 1 == $count ) { 
  35. // Found only one closing quote candidate, so give it priority over primes. 
  36. $sentence = str_replace( $flag, $close_quote, $sentence ); 
  37. $sentence = preg_replace( $prime_pattern, $prime, $sentence ); 
  38. } else { 
  39. // No closing quotes found. Just run primes pattern. 
  40. $sentence = preg_replace( $prime_pattern, $prime, $sentence ); 
  41. } else { 
  42. $sentence = preg_replace( $prime_pattern, $prime, $sentence ); 
  43. $sentence = preg_replace( $quote_pattern, $close_quote, $sentence ); 
  44. if ( '"' == $needle && false !== strpos( $sentence, '"' ) ) { 
  45. $sentence = str_replace( '"', $close_quote, $sentence ); 
  46.  
  47. return implode( $open_quote, $sentences );