获取

2014-07-11 50 views
1

我想h1标签的所有值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"); 
?> 

但它没有运行,我得到:

通知:Array对字符串的转换在C:\ XAMPP \ htdocs中\检查\ abc.php 第14行阵列

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

+0

'getTextBetweenTags'返回一个数组。使用'print_r()'来检查结果 – user1978142

回答

1

您正在尝试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);