donqo88682
donqo88682
2014-09-27 14:54

PHP:如何使用simple_html_dom解析器将CSS text-align属性添加到元素的已存在的内联样式属性中?

I have a string variable $str which contains HTML for a part of a page. I need to add the CSS property text-align:justify to all the <p> elements in the HTML (preferably using simple_html_dom parser).

Some of the <p> elements already have an inline style attribute, others don't. So I can not do something like

$str_html = str_get_html($str);
foreach ($str->find('p') as $p) {
  $p->style='text-align:justify;';
}

Because this will void any other styles already applied to the <p> element.

PS:- I can not use JQuery. I can use JS but will only use it as a last resort.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongmu1914 dongmu1914 7年前

    You could concatenate instead of re-assigning

    $str_html = str_get_html($str);
    foreach ($str->find('p') as $p) {
      $p->style .= '; text-align:justify;';
    }
    
    点赞 评论 复制链接分享