public function style(DOMElement &$element, $name, $val = '')
    {
        // Get all styles from the element
        $elementStyle = $this->attr($element, 'style');
        $elementStyle = trim($elementStyle, '; ');

        $styleArray = [];
        if (!empty($elementStyle)) {
            $styleArray = explode(';', $elementStyle);
        }
        $styles = [];
        foreach ($styleArray as $stylePair) {
            $pair = explode(':', $stylePair);
            $styles[trim($pair[0])] = trim($pair[1]);
        }

        // If value is null or false, remove attribute
        if (is_null($val) || (is_bool($val) && $val === false)) {
            unset($styles[$name]);
        } else if (empty($val)) {
            if (isset($styles[$name])) {
                return $styles[$name];
            }

            return null;
        } else {
            $styles[$name] = $val;
        }

        // Pack all styles into one value
        $styleArray = [];
        foreach ($styles as $name => $value) {
            $pieces = [$name, $value];
            $styleArray[] = implode(': ', $pieces);
        }
        $elementStyle = implode('; ', $styleArray);

        // Set style attribute
        $this->attr($element, 'style', (empty($elementStyle) ? null : $elementStyle));

        // Return the HTML element
        return $element;
    }
public function style(DOMElement &$element, $name, $val = '')
    {
        // Get all styles from the element
        $elementStyle = $this->attr($element, 'style');
        $elementStyle = trim($elementStyle, '; ');

        $styleArray = [];
        if (!empty($elementStyle)) {
            $styleArray = explode(';', $elementStyle);
        }
        $styles = [];
        foreach ($styleArray as $stylePair) {
            $pair = explode(':', $stylePair);
            $styles[trim($pair[0])] = trim($pair[1]);
        }

        // If value is null or false, remove attribute
        if (is_null($val) || (is_bool($val) && $val === false)) {
            unset($styles[$name]);
        } else if (empty($val)) {
            if (isset($styles[$name])) {
                return $styles[$name];
            }

            return null;
        } else {
            $styles[$name] = $val;
        }

        // Pack all styles into one value
        $styleArray = [];
        foreach ($styles as $name => $value) {
            $pieces = [$name, $value];
            $styleArray[] = implode(': ', $pieces);
        }
        $elementStyle = implode('; ', $styleArray);

        // Set style attribute
        $this->attr($element, 'style', (empty($elementStyle) ? null : $elementStyle));

        // Return the HTML element
        return $element;
    }
public function style ( DOMElement & $ element , $ name , $ val = '' ) { // Get all styles from the element $ elementStyle = $ this -> attr ( $ element , 'style' ) ; $ elementStyle = trim ( $ elementStyle , '; ' ) ; $ styleArray = [ ] ; if ( ! empty ( $ elementStyle ) ) { $ styleArray = explode ( ';' , $ elementStyle ) ; } $ styles = [ ] ; foreach ( $ styleArray as $ stylePair ) { $ pair = explode ( ':' , $ stylePair ) ; $ styles [ trim ( $ pair [ 0 ] ) ] = trim ( $ pair [ 1 ] ) ; } // If value is null or false, remove attribute if ( is_null ( $ val ) || ( is_bool ( $ val ) && $ val === false ) ) { unset ( $ styles [ $ name ] ) ; } else if ( empty ( $ val ) ) { if ( isset ( $ styles [ $ name ] ) ) { return $ styles [ $ name ] ; } return null ; } else { $ styles [ $ name ] = $ val ; } // Pack all styles into one value $ styleArray = [ ] ; foreach ( $ styles as $ name => $ value ) { $ pieces = [ $ name , $ value ] ; $ styleArray [ ] = implode ( ': ' , $ pieces ) ; } $ elementStyle = implode ( '; ' , $ styleArray ) ; // Set style attribute $ this -> attr ( $ element , 'style' , ( empty ( $ elementStyle ) ? null : $ elementStyle ) ) ; // Return the HTML element return $ element ; }
Set or get a style value.
This function will append the style rule in the style attribute.

@param DOMElement $element The DOMElement to handle.
@param string     $name    The style name.
@param string     $val     If the value is NULL or FALSE, the value is considered negative and the style will be
removed from the attribute. If the value is empty string (default, null is not
included), the function will return the style value. Otherwise, the style will be
appended to the style attribute and the new attribute will be returned.

@return DOMElement|mixed|null
@throws Exception
Set or get a style value . This function will append the style rule in the style attribute .