0

Using http://simplehtmldom.sourceforge.net/manual.htm, is it possible to extract the inline CSS of a HTML file?

This parser if pure html, assuming (just correct me if I'm wrong) it can't parse CSS tags.Is there any other way to extract inline CSS in the html file?

5
  • Do you want to parse inline css (inside style elements) or inline styles (inside style attribute on elements). Commented Jan 22, 2011 at 17:13
  • 1
    Inline css is part of the html. It is just the contents of the style tag, or the value of the style attribute. So you should be able to get those values using an HTML parser. Commented Jan 22, 2011 at 17:15
  • inline is always referred to as inside style attributes of elements, the former would be in-document styles Commented Jan 22, 2011 at 17:16
  • can i include css parsing it using the parser? Commented Jan 22, 2011 at 17:29
  • Since you already learned how to replace attributes, you already know how to fetch the style attribute, so this is a duplicate of Using SimpleHtmlDom, how to remove and replace a specific attribute. Commented Jan 25, 2011 at 10:07

4 Answers 4

3

If you really need to use simplehtmldom to extract this :


For

<style>
...
</style>

Use :

$css = $html->find('style')->innertext;

For

<div style="background:blue; color:white;"></div>

Use :

$css = $html->find('div[style]')->style;

If there's more than one div with style atribute or more than one <style>, you can use a foreach to loop between them.


To parse styles :

in PHP :

$s = 'background:blue; color:white;';

$results = [];
$styles = explode(';', $s);

foreach ($styles as $style) {
    $properties = explode(':', $style);
    if (2 === count($properties)) {
        $results[trim($properties[0])] = trim($properties[1]);
    }
}

var_dump($results);

in JS

let s = 'background:blue; color:white;';

let results = {};
let styles = s.split(";");

for (let style in styles) {
    let properties = styles[style].split(":");
  if (properties.length === 2) {
    results[properties[0].trim()] = properties[1].trim();
  }
}

console.log(results);

https://jsfiddle.net/zyfhtwj2/

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, can you please tell me if I want to get only color attribute, how will I do it???
1

This should work:

$doc = new DOMDocument();
$doc->loadHTML('<html><body style="color: red"></body></html>');

$els = $doc->getElementsByTagName('*');

for($i = 0; $i < $els->length; $i++)
  echo $els->item($i)->getAttribute('style'); // color: red

Comments

1

Using a normal HTML parser:

  1. Walk through all elements
  2. If element's tag is style, get the element's content
  3. If the element has an attribute style, get that attribute's value.

Comments

0
find('css','div[style*="display:block;"] ')

That way you can find the style with attribute display:block. Modify this as you want and you can get inline CSS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.