替换HTML标记内容

时间:2012-02-18 14:30:16

标签: php html

创建替换HTML元素内容的函数的最佳方法是什么?

我已经研究过使用正则表达式,但是后来发现这种方法由于持久性问题而不适合。 PHP需要通过HTML循环才能找到所有/任何匹配项。

我需要创建一个除params之外的函数。一个是元素的标签名称(),另一个是元素类名(class =“someclass”)

如果设置了一个类,那么我需要用新内容替换该标记和类名设置的所有元素。

如果尚未设置类,则应该只搜索标记名称。

<div>Replace Element Tag Contents</div>
<div class="someclass">Replace Element Contents That uses specified class</div>

非常感谢任何帮助。 如果这没有意义,请告诉我!

1 个答案:

答案 0 :(得分:1)

我会使用jQuery,除非你因某些原因特别需要使用php。

$('div.someclass').text('this will replace the text in all div's with the class someclass').removeClass('someclass').addClass('Your new class name');

我也会读这个 http://api.jquery.com/text/

如果你需要使用PHP,那么已经构建了一个非常棒的dom解析器。 http://simplehtmldom.sourceforge.net/

相关问题