2

Here is example which I have try

<?php
 include 'spider/classes/simple_html_dom.php';
 $html = new simple_html_dom();
 $html->load("<html><body><h2>Heading 1</h2><h2>This heading 2</h2></p></p></body></html>");
 $e = $html->find("h2", 0);
 $key = array();
 if($e->plaintext != ""){
 foreach($html->find("h2", 0) as $e){
    //echo $e->plaintext;
    array_push($key, $e->plaintext);
  }
 } else {
     echo "error";
 }
 print_r($key);
 ?>

Result:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => Heading 1This heading 2

[6] => [7] => )

How Do i use array_push to create an array?

2 Answers 2

3

What happens, when you try this code? I removed the first "find" and i also found an example on the internet, where the second param of "find" was not set.

<?php
 include 'spider/classes/simple_html_dom.php';
 $html = new simple_html_dom();
 $html->load("<html><body><h2>Heading 1</h2><h2>This heading 2</h2></p></p></body></html>");
 $key = array();
 if(isset($html)){
 foreach($html->find("h2") as $e){
    //echo $e->plaintext;
    array_push($key, $e->plaintext);
  }
 } else {
     echo "error";
 }
 print_r($key);
 ?>

Explanation:

// Find all anchors, returns a array of element objects
$ret = $html->find('a');

// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);
Sign up to request clarification or add additional context in comments.

Comments

1

Here's an alternative with the default DOMDocument class.

$html = new DOMDocument('1.0','utf-8');
$html->loadHTML("<html><body><h2>Heading 1</h2><h2>This heading 2</h2></p></p></body></html>");

$key = array();
$h2 = $html->getElementsByTagName('h2');

for ($i = 0; $i < $h2->length; $i++) {
    array_push($key, $h2->item($i)->nodeValue);
}
print_r($key);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.