Solution 1 :

$string = '<p><span style="font-style-italic;font-weight:bold">abcde</span><span style="font-weight:bold">abcde</span></p>';

$dom = new DOMDocument();
$dom->loadHTML($string);

$xp = new DOMXPath($dom);

$str = '';
$results = $xp->query('//span');
if($results->length>0){
    foreach($results as $result){
        $style = $result->getAttribute("style");
        $style_arr = explode(";",$style);
        $style_template = '%s';
        if(count($style_arr)>0){
            foreach($style_arr as $style_item){
                if($style_item == 'font-style-italic'){
                    $style_template = '<i>'.$style_template.'</i>';
                }
                if($style_item == 'font-weight:bold'){
                    $style_template = '<b>'.$style_template.'</b>';
                }
            }
        }
        $str .= sprintf($style_template,$result->nodeValue);
    }
}
$str = '<p>'.$str.'</p>';

Solution 2 :

You can also use html tags under php parameters or php opening and closing tags like this

<?php
    echo"<h1>Here is Heading h1 </h1>"; 
 ?>

Or you can Put your html code in ” ” after echo
Like this

<?php
echo"Your Html Code Here";
?>

Solution 3 :

$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $input);

Match a < follow by one or more and not > until space came and the style="anything" reached. The /i will work with capital STYLE and $1 will leave the tag as it is, if the tag does not include style="". And for the single quote style='' use this:

(<[^>]+) style=("|').*?("|')

Problem :

As title, If I have some html <p><span style="font-style:italic">abcde</span><span style="font-weight:bold">abcde</span></p>, I want to strip the style tags and transform them into html tags, so to make it become <p><i>abcde</i><b>abcde</b></p>. How can I do that in PHP?

I notice that when I open the html in CKEditor, this kind of transformation is done automatically. But I want to do it in backend PHP. Thanks.

Comments

Comment posted by phpliveregex.com

have you try with

Comment posted by stackoverflow.com/help/answering

Welcome to SO. Please explain your answer to improve its quality (see

Comment posted by Nigel Ren

Can you please explain how this solves the problem?

Comment posted by Quentin

This doesn’t appear to be remotely related to the question that was asked.

Comment posted by Nigel Ren

Not sure if something like

By