如何将类添加到DOM中的img对象中?

时间:2021-08-08 19:35:25

Here is piece of code

这是一段代码。

$doc = new DOMDocument();
$doc->loadHTML($article_header);

$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {

Into $imgs goes DOM img tag. Now I want to change the original img tag by adding some class to it.

进入$img的是DOM img标签。现在我想通过添加一些类来更改原始的img标记。

SO if the $article_header was this:

如果$article_header是这样的:

"some text"...<img src = 'http://some_source'>...some text...

Now I want it to become this:

现在我想让它变成这样:

"some text"...<img class = 'someclass' src = 'http://some_source'>...some text...

UPDATE

更新

I repeat. Start variable is $article_header. So all changes must be done to it.

我再说一遍。变量是article_header美元开始。所以所有的改变都必须对它进行。

With my code I just search through $article_header for img tags , finding them putting them into some variables and change them there is ok, but how can I put all changes back to $article_header ???

对于我的代码,我只是在$article_header中搜索img标记,找到它们并将它们放入一些变量中并对它们进行更改,这是可以的,但是我如何将所有更改返回到$article_header呢??

3 个解决方案

#1


10  

In your foreach loop, call $img->setAttribute('class', 'someclass');. This should do the trick. See more at http://docs.php.net/manual/en/domelement.setattribute.php

在foreach循环中,调用$img->setAttribute('class', 'someclass');这应该能达到目的。多见于http://docs.php.net/manual/en/domelement.setattribute.php

Then you need to save the modified document back using $article_header = $doc->saveXml();.

然后需要使用$article_header = $doc->saveXml()将修改后的文档保存回来;

#2


8  

If you know that the element will not have a class set already you can just use DOMElement::setAttribute(), like:

如果您知道该元素还没有一个类集,您可以使用DOMElement::setAttribute(),比如:

$img->setAttribute('class','someClass');

If you are not sure if the element might already have a class set, then you should do a getAttribute() first and then add your class to the list of classes.

如果您不确定元素是否已经有一个类集,那么您应该先做一个getAttribute(),然后将您的类添加到类的列表中。

#3


0  

Is this what you were looking for: http://php.net/manual/en/domelement.setattribute.php

这就是您要查找的:http://php.net/manual/en/domelement.setattribute.php吗

#1


10  

In your foreach loop, call $img->setAttribute('class', 'someclass');. This should do the trick. See more at http://docs.php.net/manual/en/domelement.setattribute.php

在foreach循环中,调用$img->setAttribute('class', 'someclass');这应该能达到目的。多见于http://docs.php.net/manual/en/domelement.setattribute.php

Then you need to save the modified document back using $article_header = $doc->saveXml();.

然后需要使用$article_header = $doc->saveXml()将修改后的文档保存回来;

#2


8  

If you know that the element will not have a class set already you can just use DOMElement::setAttribute(), like:

如果您知道该元素还没有一个类集,您可以使用DOMElement::setAttribute(),比如:

$img->setAttribute('class','someClass');

If you are not sure if the element might already have a class set, then you should do a getAttribute() first and then add your class to the list of classes.

如果您不确定元素是否已经有一个类集,那么您应该先做一个getAttribute(),然后将您的类添加到类的列表中。

#3


0  

Is this what you were looking for: http://php.net/manual/en/domelement.setattribute.php

这就是您要查找的:http://php.net/manual/en/domelement.setattribute.php吗