I know this is an old question, but if you ever want to fix the incorrect & &; characters in your HTML. You can use code similar to this:
$page = file_get_contents('http://www.example.com'); $page = preg_replace('/\s+/', ' ', trim($page)); fixAmps($page, 0); $dom->loadHTML($page); function fixAmps(&$html, $offset) { $positionAmp = strpos($html, '&', $offset); $positionSemiColumn = strpos($html, ';', $positionAmp+1); $string = substr($html, $positionAmp, $positionSemiColumn-$positionAmp+1); if ($positionAmp !== false) { // If an '&' can be found. if ($positionSemiColumn === false) { // If no ';' can be found. $html = substr_replace($html, '&', $positionAmp, 1); // Replace straight away. } else if (preg_match('/&(#[0-9]+|[AZ|az|0-9]+);/', $string) === 0) { // If a standard escape cannot be found. $html = substr_replace($html, '&', $positionAmp, 1); // This mean we need to escapa the '&' sign. fixAmps($html, $positionAmp+5); // Recursive call from the new position. } else { fixAmps($html, $positionAmp+1); // Recursive call from the new position. } } }
Nicolas Bouvrette Feb 15 '15 at 2:02 2015-02-15 14:02
source share