2

I recently discovered PHPDom and found it very useful to manipulate HTML code. Unfortunately the documentation online is limited and there are no comprehensive PHPDom manuals that I am aware of. (There is no PHPdom tag in stockoverflow either!!)

This is the problem I am experiencing.

I would like to change the style an html tag, this is what I am using:

            $item = $dom->getElementById($name);
            $item->setAttribute('style', 'background-color:#FF0000;');
            $item = $dom->getElementById($name.'_error');
            $item->setAttribute('style', 'display:true;'); 

However the result of this code is that the styles defined in $name and name.'_error' are overwritten.

To fix the problem, I am looking for a PHPdom method, something like editAttribute, that allows to edit an attribute without overwriting the existing style attributes for that element.

Does anyone know how to fix this problem?

user1232551
  • 53
  • 1
  • 9
  • Better check this question. http://stackoverflow.com/questions/16127142/modify-html-attribute-with-php – Sundar Jul 28 '15 at 04:10
  • That does not help as . In fact, Linda needs to replace the href as a whole, which is not my case. – user1232551 Jul 28 '15 at 05:29
  • I found the solution: `$item->setAttribute('style', $item->getAttribute('style') . 'background-color:#FF0000;');`@Sundar – user1232551 Sep 29 '15 at 07:34

1 Answers1

0

That would mean that you need to parse and edit the CSS properties in the attribute.

I implemented that in FluentDOM, actually. You can do something like:

$fd = FluentDOM($html, 'text/html');
$fd
  ->find('//a')
  ->css['text-decoration'] = 'none';
echo (string)$fd;

This logic is located in FluentDOM\Query\Css\Properties. You can use this object to edit a style property string.

$style = new FluentDOM\Query\Css\Properties($node->getAttribute('style'));
$style['background-color'] = '#FF0000';
$node->setAttribute('style', $style); 
ThW
  • 19,120
  • 3
  • 22
  • 44