Reputation: 243
I am using a Wordpress plugin that puts the code tag around text that is in backticks for highlighting purposes on my blog. Now the original function is this:
function mdc_the_content( $content ) {
$content = preg_replace( '/`(.*?)`/', '<code>$1</code>', $content );
return $content;
} // mdc_the_content
add_filter( 'the_content', 'mdc_the_content' );
When I am using js highlighter plugin (code highlighter) then it places the code tag around the code that is in backticks. I want to avoid that. Would the function below work? Only put the code tag around tags when the backticks are not wrapped in <pre class="EnlighterJSRAW" data-enlighter-language="r">$1</pre>
. Would the if statement below work? Sometimes there might be multiple objects wrapped around backticks within the highlighter. Thank you.
function mdc_the_content( $content ) {
if(!preg_match('/`(.*?)`/', '<pre class="EnlighterJSRAW" data-enlighter-language="r">$1</pre>', $content)) {
$content = preg_replace( '/`(.*?)`/', '<code>$1</code>', $content );
}
return $content;
} // mdc_the_content
add_filter( 'the_content', 'mdc_the_content' );
Upvotes: 0
Views: 50
Reputation: 626747
You can use
$content = preg_replace( '~<pre\s+class="EnlighterJSRAW"\s+data-enlighter-language="r">.*?</pre>(*SKIP)(*F)|`(.*?)`~s', '<code>$1</code>', $content );
See the regex demo.
The <pre\s+class="EnlighterJSRAW"\s+data-enlighter-language="r">.*?</pre>(*SKIP)(*F)|
part matches the pre
tag with two specific attributes allowing any amount of whitespace and all contents in it, and the `(.*?)`
will match in all other contexts.
Upvotes: 1