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();

$xp = new DOMXPath($dom);

$str = '';
$results = $xp->query('//span');
    foreach($results as $result){
        $style = $result->getAttribute("style");
        $style_arr = explode(";",$style);
        $style_template = '%s';
            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

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

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

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.


Comment posted by

have you try with

Comment posted by

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