獲取

2014-07-11 57 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);