0

I'm using a builtin method that returns an html string:

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

Where $stuff is:

<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? Or is there a easier way to do that?

Thanks!

3
  • 2
    See how $this->getBlock() build this items, or post code here Commented Nov 11, 2014 at 14:17
  • 2
    Did you check out DOMElement? php.net/manual/en/class.domelement.php Commented Nov 11, 2014 at 14:18
  • you want to insert tags after </ ul > tags ?? Commented Nov 11, 2014 at 14:19

1 Answer 1

1

Look into the following DOM functions/classes in PHP:

The DOMDocument class

DOMDocument::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 = $this->getBlock();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.