Question

There are two different tags in the HTML which are row0-1 and row1-1. I want to search for the contents in the HTML called row1Time from the row1-1 tag.

Here's and example HTML:

<li class="zc-ssl-pg" id="row0-1" style="">
    <span id="row1Time" class="zc-ssl-pg-time">4:00 PM</span>
    <li class="zc-ssl-pg" id="row1-1" style="">
    <span id="row1Time" class="zc-ssl-pg-time">3:00 PM</span>

Here's my PHP:

    <?php
    $errmsg_arr = array();
    $errflag = false;
    $link;

    function db_connect()
    {
      define('DB_HOST', 'localhost');
      define('DB_USER', 'myusername');
      define('DB_PASSWORD', 'mypassword');
      define('DB_DATABASE', 'mydbname');

      $errmsg_arr = array();
      $errflag = false;
      $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

      if(!$link) 
      {
        die('Failed to connect to server: ' . mysql_error());
      }

      $db = mysql_select_db(DB_DATABASE);
      if(!$db) 
      {
        die("Unable to select database");
      }
    }

    $links = $row['links'];
    include ('simple_html_dom.php');
    $html = file_get_html($links);
    //echo $row['links'];

    $base = $row['links'];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_URL, $base);
    curl_setopt($curl, CURLOPT_REFERER, $base);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $str = curl_exec($curl);
    curl_close($curl);

    // Create a DOM object
    $html = new simple_html_dom();
    // Load HTML from a string
    $html->load($str);

    //get all category links
    /*foreach($html_base->find('a') as $element) {
        echo "<pre>";
        print_r( $element->href );
        echo "</pre>";
    }*/

    //$html_base->clear();
    //unset($html_base);

    $time1 = $html->find('span[id=row1Time]', 0)->plaintext;
    echo '<span id="time1">'.$time1.'</span> - ';
?>

When I tried to parse the contents from the HTML using this:

$time1 = $html->find('span[id=row1Time]', 0)->plaintext;
echo '<span id="time1">'.$time1.'</span> - ';

The return output I get is from the tags row0-1 with row1Time:

4:00 PM

I want to search for the contents in the HTML from the tags row1-1 with row1Time to get the return contains 3:00 PM. Can you please help me get those contents using simple_html_dom?

Était-ce utile?

La solution

You have mistaken me with your errors... please check your html code carefully!

You want to get the text withing the span element which is inside the li node with the id 'row1-1'. This can be translated to $html->find('li#row1-1', 0)->plaintext;

This is A full working code:

$input =  <<<_DATA_
    <li class="zc-ssl-pg" id="row0-1" style="">
    <span id="row1Time" class="zc-ssl-pg-time">4:00 PM</span>
    <li class="zc-ssl-pg" id="row1-1" style="">
    <span id="row1Time2" class="zc-ssl-pg-time">3:00 PM</span>
_DATA_;

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($input);

$time1 = $html->find('li#row1-1', 0)->plaintext;

echo $time1;

// Clear DOM object
$html->clear();
unset($html);

OUTPUT

3:00 PM

Please refer to the PHP Simple HTML DOM Parser Manual for more informations...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top