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.
我有一个字符串变量 某些 因为这将使任何其他 已经应用于 PS: - 我不能使用JQuery。 我可以使用JS,但只会将它作为最后的手段。 p>
div> $ str code>,其中包含部分页面的HTML。 我需要将CSS属性
text-align:justify code>添加到HTML中的所有
&lt; p&gt; code>元素中(最好使用
simple_html_dom code>解析器) 。 p>
&lt; p&gt; code>元素已经具有内联
样式 code>属性,而其他元素则没有。 所以我不能 em>做类似 p>
$ str_html = str_get_html($ str);
foreach($ str-&gt; find('p') )作为$ p){
$ p-&gt; style ='text-align:justify;';
}
code> pre>
&lt; p&gt; code>元素的样式。 p>
You could concatenate instead of re-assigning
$str_html = str_get_html($str);
foreach ($str->find('p') as $p) {
$p->style .= '; text-align:justify;';
}