1

I want to get all value of h1 tag and I found this article: getting all values from h1 tags using php

Then I tried to using:

<?php 
include "functions/simple_html_dom.php";
function getTextBetweenTags($string, $tagname) {
    // Create DOM from string
    $html = str_get_html($string);

    $titles = array();
    // Find all tags 
    foreach($html->find($tagname) as $element) {
        $titles[] = $element->plaintext;
    }
    return $titles;
}
echo getTextBetweenTags("http://mydomain.com/","h1");
?>

But it's not running and I get:

Notice: Array to string conversion in C:\xampp\htdocs\checker\abc.php on line 14 Array

Plz help me fix it. I want to get all h1 tag of a website with input data is URL of that website. Thanks so much!

Community
  • 1
  • 1

1 Answers1

1

You're trying to echo an array, which will result into an error. And the function is little bit off. Example:

include 'functions/simple_html_dom.php';
function getTextBetweenTags($url, $tagname) {
    $values = array();
    $html = file_get_html($url);
    foreach($html->find($tagname) as $tag) {
        $values[] = trim($tag->innertext);
    }

    return $values;
}

$output = getTextBetweenTags('http://www.stackoverflow.com/', 'h1');
echo '<pre>';
print_r($output);
user1978142
  • 7,946
  • 3
  • 17
  • 20