Extract style block from submited html page content with Html Purifier

72 Views Asked by At

How to extract block style such <style></style> with html purifier. I try with below code to extract style form html content. but i cant;

$config = \HTMLPurifier_Config::createDefault();
$config->set('HTML.Doctype', 'HTML 4.01 Transitional');
$config->set('Cache.SerializerPath', storage_path('app/purifier'));
$config->set('Filter.ExtractStyleBlocks', true);
$purifier = new \HTMLPurifier($config);

$dirty = '<style>body {color:#F00;}</style> Some text';
$html = $purifier->purify($dirty);
$styles = $purifier->context->get('StyleBlocks');

var_dump($styles);
1

There are 1 best solutions below

1
shemjay On

I am not quite sure if i understand your question. Maybe you can go a bit more in depth.

From what i can see the code you have provided is correct, and it should extract the <style> blocks from the HTML content. However, the output of the extracted styles may not be as expected.

The $styles variable will contain an associative array of the extracted style blocks, where the keys are the position of the style block in the input HTML string and the values are the content of the style block. Here is an example of what I mean:

$dirty = '<style>body {color:#F00;}</style> Some text';
$html = $purifier->purify($dirty);
$styles = $purifier->context->get('StyleBlocks');

foreach ($styles as $position => $style) {
    echo "Style block at position $position: $style\n";
}

Which should give you the following:

Style block at position 0: body {color:#F00;}

If you want to use the extracted styles in your application, you can output them as inline styles in your HTML code, or you can save them to a file and include the file in your HTML code.