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
-
1I think you may have an x y problem, why do you want to do this? meta.stackexchange.com/questions/66377/what-is-the-xy-problemgbtimmon– gbtimmon2013-02-14 21:03:07 +00:00Commented Feb 14, 2013 at 21:03
-
Why bother with find/replace? Just conditionally output the HTML.Brad Koch– Brad Koch2013-02-14 21:04:48 +00:00Commented 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.Luceos– Luceos2013-02-14 21:05:51 +00:00Commented 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?Adam Baney– Adam Baney2013-02-14 21:08:41 +00:00Commented 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?Adam Baney– Adam Baney2013-02-14 21:12:36 +00:00Commented Feb 14, 2013 at 21:12
Add a comment
|
1 Answer
//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
12 Comments
Adam Baney
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.Adam Baney
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 />'));gbtimmon
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 ?
Adam Baney
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>gbtimmon
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.
|