Search code examples
phpregexwordpressinlinehtml-heading

How do I remove strong tags & inline styling from headings?


I'm trying to get rid of inline styling and strong tags within headings in wordpress. e.g. <h2 style="font-size: 18px"><strong>This is </strong><strong>mess</strong><strong>y</strong></h2>

  1. Would I be able to use a regex replace to get rid of the tags without losing the text in between the tags?

  2. Would you recommend to use the function.php to filter the tags or should I export the entire wp_posts table and do a regex replace in sublime?


Solution

  • $str='before <h2 style="font-size: 18px"><strong>This is </strong><strong>mess</strong><strong>y</strong></h2>after';
    
    $pos = strpos($str, "<h2");
    $pos2 = strpos($str, "</h2>");
    
    $partstr = substr($str, $pos, $pos2);
    $tag = substr($str, $pos, 3);
    
    $partstr2 = str_replace("<strong>" , "", $partstr);
    $partstr2 = str_replace("</strong>" , "", $partstr2);
    $partstr2 = str_replace('<h2 style="font-size: 18px">', "", $partstr2);
    
    
    $newstr = str_replace($partstr, $tag . ">".$partstr2 , $str);
    
    echo  $newstr;
    

    I would not recommend preg_replace.

    Edited code.
    Working example: http://sandbox.onlinephpfunctions.com/code/13be2bfb61de6e7d3591caf9a343011a771bab63