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

395
Vistas
PHP DOM: Get NodeValue excluding the child nodes

I have a variable$contentcontaining a piece of HTML code :

<b>AMZN 466.00 ( 15743 ) ( <span class='red'> -1 </span>) 
MSFT 290.00 ( 37296 ) ( <span class='red'> -2 </span>)
TWTR 4,000.00 ( 20 ) ( <span class=''> 0 </span>)</b>

Now, I want the values of <b>excluding the values of <span> using PHP DOM. How can this be done? A code snippet would be helpful.

So far, I've tried this:

$dom = new domDocument;
@$dom->loadHTML($content);
$contents_i_want = $dom->getElementsByTagName('b');
foreach($contents_i_want as $content_i_want){ 
   $filtered_content = $content_i_want->nodeValue;
   echo $filtered_content;
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Hope this will help you out..

Try this code snippet here

<?php
ini_set('display_errors', 1);
$string='<html><body><b>AMZN 466.00 ( 15743 ) ( <span class=\'red\'> -1 </span>) 
MSFT 290.00 ( 37296 ) ( <span class=\'red\'> -2 </span>)
TWTR 4,000.00 ( 20 ) ( <span class=\'\'> 0 </span>)</b></body></html>';

$dom = new DOMDocument();
$dom->loadHTML($string);
$dom->getElementsByTagName("b");

$xpath= new DOMXPath($dom);
$result=$xpath->query("//b/span");//here we are querying domdocument to find span which is inside b.

$nodesToRemove=array();//here we are maintaining an array of nodes which we want to remove
foreach($result as $node)
{
    $node->parentNode->removeChild($node);//removing nodes from its parent
}
 echo $dom->getElementsByTagName("b")->item(0)->textContent;//displaying content after removing nodes.

Output:

AMZN 466.00 ( 15743 ) ( ) 
MSFT 290.00 ( 37296 ) ( )
TWTR 4,000.00 ( 20 ) ( )
over 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