jetpack_preg_replace_callback_outside_tags

Runs preg_replace_callback so that replacements don't happen within open tags.

Description

(string) jetpack_preg_replace_callback_outside_tags( (string) $pattern, (string) $callback, (string) $content, (null) $search = null ); 

Parameters are the same as preg_replace, with an added optional search param for improved performance.

Returns (string)

$content Replaced post content.

Parameters (4)

0. $pattern (string)
Pattern to search for.
1. $callback (string)
Callback returning the replacement string.
2. $content (string)
Post content.
3. $search — Optional. (null) => null
String to search for.

Usage

  1. if ( !function_exists( 'jetpack_preg_replace_callback_outside_tags' ) ) { 
  2. require_once '/modules/shortcodes.php'; 
  3.  
  4. // Pattern to search for. 
  5. $pattern = ''; 
  6.  
  7. // Callback returning the replacement string. 
  8. $callback = ''; 
  9.  
  10. // Post content. 
  11. $content = ''; 
  12.  
  13. // String to search for. 
  14. $search = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = jetpack_preg_replace_callback_outside_tags($pattern, $callback, $content, $search); 
  18.  

Defined (1)

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

/modules/shortcodes.php  
  1. function jetpack_preg_replace_callback_outside_tags( $pattern, $callback, $content, $search = null ) { 
  2. if ( ! function_exists( 'wp_html_split' ) ) { 
  3. return $content; 
  4.  
  5. if ( $search && false === strpos( $content, $search ) ) { 
  6. return $content; 
  7.  
  8. $textarr = wp_html_split( $content ); 
  9. unset( $content ); 
  10. foreach ( $textarr as &$element ) { 
  11. if ( '' === $element || '<' === $element{0} ) { 
  12. continue; 
  13. $element = preg_replace_callback( $pattern, $callback, $element ); 
  14.  
  15. return join( $textarr );