获取h1标签的所有值


Get all value of h1 tag

我想获得h1标签的所有值,我发现这篇文章:使用php从h1标签获取所有值

然后我尝试使用:

<?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");
?>

但是它没有运行,我得到:

注意:数组到字符串的转换在C:'xampp'htdocs'checker'abc.phpon line 14 Array

请帮我修一下。我想得到一个网站的所有h1标签输入数据是该网站的URL。非常感谢!

您正在尝试echoarray,这将导致错误。这个函数有点偏离。例子:

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);