What are the potential pitfalls of using preg_replace_callback in PHP for manipulating HTML data?

Using preg_replace_callback in PHP for manipulating HTML data can be risky because it may not handle all cases of HTML correctly, leading to unexpected results or even security vulnerabilities such as XSS attacks. To mitigate this risk, it's recommended to use a more robust HTML parsing library like DOMDocument to manipulate HTML data safely and accurately.

// Example of using DOMDocument to manipulate HTML data safely
$html = '<div><p>Hello, <strong>world</strong>!</p></div>';

$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

// Manipulate the HTML data using DOM methods
$paragraphs = $dom->getElementsByTagName('p');
foreach ($paragraphs as $paragraph) {
    $paragraph->setAttribute('class', 'highlighted');
}

// Get the modified HTML data
$modifiedHtml = $dom->saveHTML();
echo $modifiedHtml;