Skip to content

Instantly share code, notes, and snippets.

@tott
Created September 9, 2011 10:56
Show Gist options
  • Save tott/1205944 to your computer and use it in GitHub Desktop.
Save tott/1205944 to your computer and use it in GitHub Desktop.
str_replace around pre/code tags
function str_replace_around_pre_code( $_search, $_replace, $content ) {
$new_content = '';
// deal with matches inside pre and code tags. we don't want to have links in sourcecode so only process around this.
if ( preg_match_all( "/(.*)(<(pre|code).*>.+<\/(pre|code)>)(.*)/msiU", $content, $content_split, PREG_PATTERN_ORDER ) ) {
foreach ( $content_split[0] as $cnt_key => $raw ) {
foreach( $_search as $key => $search ) {
$content_split[1][$cnt_key] = str_replace( $search, $_replace[$key], $content_split[1][$cnt_key] );
$content_split[5][$cnt_key] = str_replace( $search, $_replace[$key], $content_split[5][$cnt_key] );
}
$new_content .= $content_split[1][$cnt_key] . $content_split[2][$cnt_key] . $content_split[5][$cnt_key];
}
}
if ( !empty( $new_content ) )
return $new_content;
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment