2

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!

5
  • 1
    I think you may have an x y problem, why do you want to do this? meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Feb 14, 2013 at 21:03
  • Why bother with find/replace? Just conditionally output the HTML. Commented Feb 14, 2013 at 21:04
  • This website expects effort from your side. Please explain what you have tried so far, which links seem to match your question and how did they did not completely explain what you are looking for. As to a simple asnwer, try a preg_replace with a regular expression. Commented Feb 14, 2013 at 21:05
  • @gbtimmon - I want to do this because there's an issue with the WordPress editor when inserting a Horizontal Rule. To get around the issue, I'd like for people to be able to put 2 underscores in the editor where they want an HR to appear, then in the front-end, the 2 underscores (surrounded by P tags) will be replaced with an HR. Make sense? Commented Feb 14, 2013 at 21:08
  • @Luceos - I've tried lots of code searching including preg_replce, but nothing works. Do you know the answer? Commented Feb 14, 2013 at 21:12

1 Answer 1

3
//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

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for the reply, gbtimmon. Seems like I'd have to pass WordPress' the_content() through this function. If that's the case, this won't work so well. I have not only the_content(), but many custom fields that this would need to process.
I tried to use jQuery, but this interfered with another JS functionality. var change_hr = jQuery('#content'); change_hr.html( change_hr.html().replace(/<p>__<\/p>/g,'<hr />'));
You shouldn't be doing HTML editing on the client side unless you are dealing with dynamic content (which it doesn't seem like you are) can you post some sample code ?
The code that should be replaced is <p>__</p> within the sample below. I really appreciate your help, gbtimmon! <p>Vivamus quis quam felis, in elementum felis.</p> <p>__</p> <p>Vivamus quis quam felis, in elementum felis.</p>
But how is that code ending up in the produced html, can you give us the server side php the originally generates that html before it is being sent to the client. That is where you want to do the replace. You would not want to do it after the page has been generated, and you especially would not want to it on the client side.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.