agiannis
9/12/2019 - 8:56 AM

export css from html

labels : php , tool , export , css , html

<?php

function export_css_from_html($html)
{
    $htmlDoc = new DOMDocument();
    $htmlDoc->loadHTML($html);
    $tag_list = $htmlDoc->getElementsByTagName('*');
    $class_list = [];
    foreach ($tag_list as $tag) {
        $id = [(!empty($tag->getAttribute('id'))) ? $tag->getAttribute('id') : ''];
        $att_class = explode(' ', $tag->getAttribute('class'));
        $class_list = array_merge($class_list, $att_class, $id);
    }
    $html_output = '';
    foreach (array_unique($class_list) as $class) {
        if (empty($class)) {
            continue;
        }
        $html_output .= "." . $class . " { \r\n\r\n}\r\n\r\n";
    }
    return $html_output;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $html_output = export_css_from_html($_REQUEST['html']);
}
?>
<html>
<body>
<form method="post" style="background: #e8e8e8;width: 500px;margin: 0 auto;padding: 20px;display: block;">
    <textarea style="width: 100%;height: 400px;margin-bottom: 25px;"
              name="html"><?php echo isset($html_output) ? $html_output : ''; ?></textarea>
    <input style="font-size: 20px;width: 140px;margin: 0 auto;display: block;" type="submit" value="Extract css"/>
</form>
</body>
</html>