Skip to content

Instantly share code, notes, and snippets.

@karrikas
Last active February 24, 2021 10:45
Show Gist options
  • Save karrikas/ae2da25dfef8aa8e64840b594391b8b1 to your computer and use it in GitHub Desktop.
Save karrikas/ae2da25dfef8aa8e64840b594391b8b1 to your computer and use it in GitHub Desktop.
Symfony Crawler how to add <option> to <select> element
<?php
use Symfony\Component\DomCrawler\Crawler;
$crawler = new Crawler('<html><body><select id="my_select"></select></body></html>');
$crawler->filter('select#my_select')->each(function (Crawler $crawler) {
$node = $crawler->getNode(0);
$option = new \DOMElement('option', 'Option Name');
$node->appendChild($option);
$option->setAttribute('value', 'Option Value');
});
echo $crawler->html();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment