Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

506
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda