wp_replace_in_html_tags

函数
wp_replace_in_html_tags ( $haystack, $replace_pairs )
参数
  • (string) $haystack The text which has to be formatted.
    Required:
  • (array) $replace_pairs In the form array('from' => 'to', ...).
    Required:
返回值
  • (string) The formatted text.
定义位置
相关方法
do_shortcodes_in_html_tagswp_replace_insecure_home_urlwp_prepare_site_datawp_create_tag_wp_admin_html_begin
引入
4.2.3
弃用
-

wp_replace_in_html_tags: 这个函数只搜索和替换HTML标签中的字符串,而不触及字符串的其他部分。

只替换HTML元素中的字符或短语。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常见问题

FAQs
查看更多 >