获取h1标签的所有值

时间:2014-07-11 02:19:00

标签: php html simple-html-dom

我想获得h1标签的所有价值,我发现了这篇文章:getting all values from h1 tags using php

然后我尝试使用:

<?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.php中的数组到字符串转换   在第14行数组

Plz帮我修复它。我想获得一个带有输入数据的网站的所有h1标签是该网站的URL。非常感谢!

1 个答案:

答案 0 :(得分:1)

您正试图echo array,这将导致错误。功能有点偏。例如:

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