How can preg_replace and regex be used to modify HTML tags in PHP?
To modify HTML tags in PHP using preg_replace and regex, you can search for specific patterns within the HTML code using regex and then use preg_replace to replace or modify those patterns. This can be useful for tasks such as changing attributes, adding classes, or wrapping elements with additional tags.
$html = '<div class="old">Old content</div>';
$newHtml = preg_replace('/<div class="old">(.*)<\/div>/', '<div class="new">New content</div>', $html);
echo $newHtml;