Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

504
Visualizações
PHP How to avoid this warning: DOMDocument::loadHTML(): Invalid char in CDATA

I'm trying to collect some info from a web service, but I'm having issues with the CDATA Section of a page, because everything goes right when I use something like this:

$url = 'http://www.example.com';
$content = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($content);   

foreach($doc->getElementsByTagName('h3') as $subtitle) {
    echo $subtitle->textContent; //The output is the Subtitle/s. 
}

But when the page contains CDATA sections there is a problem with this error on the line $doc->loadHTML($content).

Warning: DOMDocument::loadHTML(): Invalid char in CDATA

I've seen over here a solution that I tried to implement without any success.

function sanitize_html($content) {
  if (!$content) return '';
  $invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
  return preg_replace($invalid_characters,'', $content);
}

$url = 'http://www.example.com';
$content = file_get_contents($url);
$cleanContent = sanitize_html($content);
$doc = new DOMDocument();
$doc->loadHTML($cleanContent); //Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity

But I got this other error:

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity

What could be a good way to deal with the CDATA sections of a page? Greetings.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try adding PCLZIP before load IOFactory as shown:

require_once '/Classes/PHPExcel.php';
 \PHPExcel_Settings::setZipClass(\PHPExcel_Settings::PCLZIP);   
about 4 years ago · Santiago Trujillo Relatório

0

The solution is to - replace the & symbol with &amp; or if you must have that & as it is then, may be you could enclose it in: <![CDATA[ - ]]>

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda