wp_replace_in_html_tags() WordPress Function
The wp_replace_in_html_tags() function is a tool for replacing text in HTML tags. It can be used to replace text in any attribute of an HTML tag, including the tag itself. The function takes two parameters: the first is the text to be replaced, and the second is the replacement text. The function can be used to replace text in any number of tags, and can be used multiple times on the same page.
wp_replace_in_html_tags( string $haystack, array $replace_pairs ) #
Replaces characters or phrases within HTML elements only.
Parameters
- $haystack
(string)(Required)The text which has to be formatted.
- $replace_pairs
(array)(Required)In the form array('from' => 'to', ...).
Return
(string) The formatted text.
Source
File: wp-includes/formatting.php
function wp_replace_in_html_tags( $haystack, $replace_pairs ) { // Find all elements. $textarr = wp_html_split( $haystack ); $changed = false; // Optimize when searching for one item. if ( 1 === count( $replace_pairs ) ) { // Extract $needle and $replace. foreach ( $replace_pairs as $needle => $replace ) { } // Loop through delimiters (elements) only. for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) { if ( false !== strpos( $textarr[ $i ], $needle ) ) { $textarr[ $i ] = str_replace( $needle, $replace, $textarr[ $i ] ); $changed = true; } } } else { // Extract all $needles. $needles = array_keys( $replace_pairs ); // Loop through delimiters (elements) only. for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) { foreach ( $needles as $needle ) { if ( false !== strpos( $textarr[ $i ], $needle ) ) { $textarr[ $i ] = strtr( $textarr[ $i ], $replace_pairs ); $changed = true; // After one strtr() break out of the foreach loop and look at next element. break; } } } } if ( $changed ) { $haystack = implode( $textarr ); } return $haystack; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.2.3 | Introduced. |