问题
Is there a way to use PHP to find some HTML within a page, and replace that HTML with other HTML? I'd like for this function to be in a separate functions.php file, and look at each page on page-load for the HTML that needs replacing (unless this isn't as efficient as some other method). Thanks!
回答1:
//function to add to your function definitions.
function print_processed_html($string)
{
$search = Array("--", "*", "\*", "^", "\^");
$replace = Array("<\hr>", "<b>", "<\b>", "<sup>", "<\sup>");
$processed_string = str_replace($search, $replace , $string);
echo $processed_string;
}
// outputing the string to the page.
<?php
$the_content = get_the_content();
print_processed_html($the_content);
?>
also consider reading through this http://codex.wordpress.org/Function_Reference/the_content for some tips on using the the_content() function
来源:https://stackoverflow.com/questions/14883967/use-php-to-replace-html-with-html