Preluare valoare din tag html dupa id in php

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
Stefan
Mesaje: 117

Preluare valoare din tag html dupa id in php

Salut, vreau sa preiau valoare unui tag de pe un website, am gasit un cod care ia toate valorile din input-uri. Cum as putea sa il modific ca sa ia doar valoarea de la tag-ul cu id-ul care ma intereseaza pe mine? Am incercat sa rezolv dar nu prea le am cu OOP

Cod: Selectaţi tot

<?php
function get_input_tags($html){
    $post_data = array();
     
    // a new dom object
    $dom = new DomDocument; 
     
    //load the html into the object
    $dom->loadHTML($html); 
    //discard white space
    $dom->preserveWhiteSpace = false; 
     
    //all input tags as a list
    $input_tags = $dom->getElementsByTagName('input'); 
 
    //get all rows from the table
    for ($i = 0; $i < $input_tags->length; $i++) 
    {
        if( is_object($input_tags->item($i)) )
        {
            $name = $value = '';
            $name_o = $input_tags->item($i)->attributes->getNamedItem('name');
            if(is_object($name_o))
            {
                $name = $name_o->value;
                 
                $value_o = $input_tags->item($i)->attributes->getNamedItem('value');
                if(is_object($value_o))
                {
                    $value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
                }
                 
                $post_data[$name] = $value;
            }
        }
    }
     
    return $post_data;
}
 
/*
    Usage
*/
 
error_reporting(~E_WARNING);
$html = file_get_contents("https://www.google.ro");
 
echo "<pre>";
print_r(get_input_tags($html));
echo "</pre>";
?>

MarPlo Mesaje: 4343
Salut,
Daca iti e de folos, functia getCntId() din codul urmator returneaza un array cu numele tag-ului si continutul elementului cu id-ul transmis dintr-un sir HTML in PHP.

Cod: Selectaţi tot

<?php
function getCntId($html, $id){
  $dom = new DomDocument; 
   
  //load the html into the object
  error_reporting(~E_WARNING); // to not output warning errors
  $dom->loadHTML($html);

  // get the element with id="$id"
  $elm = $dom->getElementById($id);

  return ['tag'=>$elm->tagName, 'cnt'=>$elm->nodeValue];
}
 
//Test
$html = file_get_contents('https://marplo.net/');

echo '<pre>';
var_export(getCntId($html, 'p_content'));
echo '</pre>';

Stefan Mesaje: 117
Multumesc mult! Dar pentru atributul "name" din input (in loc de ID) ce modificare ar trebui sa fac?

MarPlo Mesaje: 4343
Poti sa foloseti metoda getAttribute() ca sa verifici elementele cu un anumit atribut.
- Daca vrei sa inveti despre PHP HTML DOM vezi documentatia de la: PHP - Document Object Model

- De exemplu, poti sa folosesti aceasta functie pentru elemente <input> cu un anumit 'name':

Cod: Selectaţi tot

//receives string $html with html code
//returns the value of the html input with name of $name
function getInpValName($html, $name){
  $re ='no val';
  $dom = new DomDocument; 
   
  //load the html into the object
  error_reporting(~E_WARNING); // to not output warning errors
  $dom->loadHTML($html);
  $elms = $dom->getElementsByTagName('input');
  foreach($elms as $elm){
    if($elm->getAttribute('name') ==$name){
      $re = $elm->getAttribute('value');
      break;
    }
  }

  return $re;
}

Subiecte similare