generate functionMon, 07 Nov 2022

replace first occurrence of "string" in nodeValue with <a> with DOMDocument

$doc = new DOMDocument(); $doc->load($file); $xpath = new DOMXPath($doc); foreach($xpath->query("//node()[contains(., 'string')]") as $node) { $node->nodeValue = preg_replace('/string/', '<a>', $node->nodeValue, 1); } $doc->save($file);

Questions about programming?Chat with your personal AI assistant