wp_replace_in_html_tags

The timeline below displays how wordpress function wp_replace_in_html_tags has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Replaces characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack      The text which has to be formatted.
 * @param array  $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 (str_contains($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 (str_contains($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;
}

WordPress Version: 6.1

/**
 * Replaces characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack      The text which has to be formatted.
 * @param array  $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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;
}

WordPress Version: 5.5

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack      The text which has to be formatted.
 * @param array  $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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;
}

WordPress Version: 4.4

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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;
}

WordPress Version: .30

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 delimeters (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 delimeters (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;
}

WordPress Version: 2.3

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
function wp_replace_in_html_tags($haystack, $replace_pairs)
{
    // Find all elements.
    $comments = '!' . '(?:' . '-(?!->)' . '[^\-]*+' . ')*+' . '(?:-->)?';
    // End of comment. If not found, match all input.
    $regex = '/(' . '<' . '(?(?=!--)' . $comments . '|' . '[^>]*>?' . ')' . ')/s';
    $textarr = preg_split($regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
    $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 delimeters (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 delimeters (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;
}

WordPress Version: 1.7

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 delimeters (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 delimeters (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;
}

WordPress Version: 1.6

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
function wp_replace_in_html_tags($haystack, $replace_pairs)
{
    // Find all elements.
    $comments = '!' . '(?:' . '-(?!->)' . '[^\-]*+' . ')*+' . '(?:-->)?';
    // End of comment. If not found, match all input.
    $regex = '/(' . '<' . '(?(?=!--)' . $comments . '|' . '[^>]*>?' . ')' . ')/s';
    $textarr = preg_split($regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
    $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 delimeters (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 delimeters (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;
}

WordPress Version: 0.7

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 delimeters (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 delimeters (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;
}

WordPress Version: 0.6

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
function wp_replace_in_html_tags($haystack, $replace_pairs)
{
    // Find all elements.
    $comments = '!' . '(?:' . '-(?!->)' . '[^\-]*+' . ')*+' . '(?:-->)?';
    // End of comment. If not found, match all input.
    $regex = '/(' . '<' . '(?(?=!--)' . $comments . '|' . '[^>]*>?' . ')' . ')/s';
    $textarr = preg_split($regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
    $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 delimeters (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 delimeters (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;
}

WordPress Version: 9.8

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 delimeters (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 delimeters (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;
}

WordPress Version: 9.7

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
function wp_replace_in_html_tags($haystack, $replace_pairs)
{
    // Find all elements.
    $comments = '!' . '(?:' . '-(?!->)' . '[^\-]*+' . ')*+' . '(?:-->)?';
    // End of comment. If not found, match all input.
    $regex = '/(' . '<' . '(?(?=!--)' . $comments . '|' . '[^>]*>?' . ')' . ')/s';
    $textarr = preg_split($regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
    $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 delimeters (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 delimeters (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;
}

WordPress Version: .10

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 delimeters (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 delimeters (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;
}

WordPress Version: 8.9

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
function wp_replace_in_html_tags($haystack, $replace_pairs)
{
    // Find all elements.
    $comments = '!' . '(?:' . '-(?!->)' . '[^\-]*+' . ')*+' . '(?:-->)?';
    // End of comment. If not found, match all input.
    $regex = '/(' . '<' . '(?(?=!--)' . $comments . '|' . '[^>]*>?' . ')' . ')/s';
    $textarr = preg_split($regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
    $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 delimeters (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 delimeters (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;
}

WordPress Version: .10

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 delimeters (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 delimeters (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;
}

WordPress Version: 7.9

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
function wp_replace_in_html_tags($haystack, $replace_pairs)
{
    // Find all elements.
    $comments = '!' . '(?:' . '-(?!->)' . '[^\-]*+' . ')*+' . '(?:-->)?';
    // End of comment. If not found, match all input.
    $regex = '/(' . '<' . '(?(?=!--)' . $comments . '|' . '[^>]*>?' . ')' . ')/s';
    $textarr = preg_split($regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE);
    $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 delimeters (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 delimeters (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;
}

WordPress Version: .10

/**
 * Replace characters or phrases within HTML elements only.
 *
 * @since 4.2.3
 *
 * @param string $haystack The text which has to be formatted.
 * @param array $replace_pairs In the form array('from' => 'to', ...).
 * @return string The formatted text.
 */
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 delimeters (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 delimeters (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;
}