Get value from a Span field with cURL Parser

1

I have a question and a problem. Well it's the following with cURL I login beauty up so far .. ok but I need to get some information that is inside the html:

<span id="Number">12345678993</span>
<span id="holderName">RAFAELA VERCOSA MARIANO</span>
<span id="expirationDate">11/2019</span>

The function I am using is:

function pegar_oneclick(){
   $nome = '';
   $html = str_get_html($this->http_response);
   foreach($html->getElementById('holderName') as $element){
      $text   = $element->plaintext;
      $text   = str_replace(array("\r", "\n", "\t"), ' ', $text);
      $nome .= str_replace('  ', '', $text);
   }

   return $nome;
}

I want to return only the Number , holderName: RAFAELA VERCOSA MARIANO and expirationDate , I do not want anything ready just an orientation of like I should do.

    
asked by anonymous 05.02.2015 / 21:21

1 answer

1

You should use getElementsByTagName instead of getElementById .

See an example using DOM :

$DOM = new DOMDocument;
$DOM->loadHTML($html);
$items = $DOM->getElementsByTagName('span');

// Imprimindo os valores
for ($i = 0; $i < $items->length; $i++)
    echo $items->item($i)->nodeValue . PHP_EOL;

// Saída:
// 12345678993
// RAFAELA VERCOSA MARIANO
// 11/2019

DEMO

Your code did not work because through foreach you searched only for id holderName . Your code should look like this:

$DOM = new DOMDocument;
$DOM->loadHTML($html);

$number = $DOM->getElementById('Number')->nodeValue;
$holdername = $DOM->getElementById('holderName')->nodeValue;
$expirationdate = $DOM->getElementById('expirationDate')->nodeValue;

echo $number. PHP_EOL;         // 12345678993
echo $holdername. PHP_EOL;     // RAFAELA VERCOSA MARIANO
echo $expirationdate. PHP_EOL; // 11/2019

DEMO

    
05.02.2015 / 21:48