Use PHP to Replace HTML with HTML

笑着哭i 提交于 2019-12-23 03:57:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!