简体   繁体   English

将html标记插入php html字符串

[英]insert html markup to php html string

I'm using a builtin method that returns an html string: 我正在使用一个返回html字符串的内置方法:

var $stuff = $this->getBlock();

Where $stuff is: 其中$ stuff是:

<p>Lorem ipsum</p>
<ul>
    <li>Foo<span>Bar</span></li>
    <li>Foo<span>Bar</span></li>
    <li>Foo<span>Bar</span></li>
    .
    .
    .
</ul>

What I'd like to do is add two more list elements to the end of that list. 我想做的是在该列表的末尾添加两个列表元素。 How can I do that? 我怎样才能做到这一点? Would I just tokenize $stuff? 我将标记$ stuff吗? Or is there a easier way to do that? 还是有更简单的方法做到这一点?

Thanks! 谢谢!

Look into the following DOM functions/classes in PHP: 查看PHP中的以下DOM函数/类:

The DOMDocument class DOMDocument类

DOMDocument::createElement DOM文档::的createElement

Here's an example of how to do what you are asking: 这是一个如何执行您所要求的示例:

$stuff = "<p>Lorem ipsum</p><ul><li>Foo<span>Bar</span></li><li>Foo<span>Bar</span>  </li><li>Foo<span>Bar</span></li></ul>";

$dom = new DOMDocument();
$dom->loadHTML($stuff);

$element1 = $dom->createElement('li', 'test');
$element2 = $dom->createElement('li', 'test');

$list = $dom->getElementsByTagName('ul');

$list->item(0)->appendChild($element1);
$list->item(0)->appendChild($element2);

echo $dom->saveHTML();

You can replace $stuff with 您可以将$ stuff替换为

$stuff = $this->getBlock();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM