<?php
// Create a DOM Document
$dom = new DomDocument();
// Load your HTML
$dom->loadHTML('<form class="search-form">
<meta itemprop="target">
<input type="search">
<input type="submit">
</form>');
// Create a new <span>
$span = $dom->createElement('span', 'hello');
// Grab the <input elements (we dont have an ID)
$inputs = $dom->getElementsByTagName('input');
// Add the <span> between the inputs
$inputs->item(0)->parentNode->insertBefore($span, $inputs->item(1));
// By default when you loadHTML(), it generates doctype, html, head, and body tags. remove them!
$dom->removeChild($dom->doctype);
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
// Finally get the HTML
$html = $dom->saveHTML();
// And output / return / whatever
echo $html;
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
<form class="search-form">
<meta itemprop="target">
<input type="search">
<span>hello</span><input type="submit">
</form>