skip to Main Content

I want to change every css rule in a string whitch I extracted from a HTML email body.
The string contains css inside a element whitch influence my website styling.

I want to add an element (div class) to every css rule inside that string.
Is this possible with php?

Example:

$string = '<style type="text/css">body { blah blah } .div1 { blah blah } .div2 { blah blah }</style> Blah blah blah body text blah blah';

$extractcss = strip_tags($string , '<style>');

I want to add .mydiv to every css rule to get this:

$extractcss = '.mydiv body { blah blah } .mydiv .div1 { blah blah } .mydiv .div2 { blah blah }';

With the new string I want to influence the styling of the email body so that it no longer has any effect on my website styling.

Thanks in advance!

2

Answers


  1. Chosen as BEST ANSWER

    With te help of this topic I have the solution for my problem.

    With the following function I parse the CSS from a string. After that I make a new css string with a div class in front of every css rule.

    function parse_css($cssstring){
                                preg_match_all( '/(?ims)([a-z0-9s.:#_-@,]+){([^}]*)}/', $cssstring, $arr);
                                $result = array();
                                foreach ($arr[0] as $i => $x){
                                    $selector = trim($arr[1][$i]);
                                    $rules = explode(';', trim($arr[2][$i]));
                                    $rules_arr = array();
                                    foreach ($rules as $strRule){
                                        if (!empty($strRule)){
                                            $rule = explode(":", $strRule);
                                            $rules_arr[trim($rule[0])] = trim($rule[1]);
                                        }
                                    }
                                    
                                    $selectors = explode(',', trim($selector));
                                    foreach ($selectors as $strSel){
                                        $result[$strSel] = $rules_arr;
                                    }
                                }
                                return $result;
                            }
    
    // Extract and change css style
                            if ( str_contains($message, '<style') ) { 
                            
                                $extractcss = strip_tags($message, '<style>');
                                $parsecss = parse_css($extractcss);
                                $newcssstring = '<style type="text/css">';
                            
                                foreach ( $parsecss as $key => $value ) {
                                    $newcssstring .= '.message-body '. $key .' { ';
                                        
                                        foreach ( $value as $k => $v ) {
                                            $newcssstring .= $k .': '. $v .'; ';
                                        }
    
                                    $newcssstring .= ' } ';
                                }
                                $newcssstring .= '</style>';
    
                                $message = preg_replace("#([<]style)(.*)([<]/style[>])#s", "<!-- style extracted -->", $message);
    
                                $message= $newcssstring . $message;
    
                            }
    

  2. The code have some error, anyway the simpliest way is to use the str_replace() php function

    $newstring=str_replace("{",".mydiv {",$string);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search