2017-10-19 64 views
-1

我意識到這個問題之前已經問過,因爲我已經經歷了10多個類似的問題,但我想弄清楚我做錯了什麼。我不斷收到錯誤警告:DOMDocument :: LoadHTML:空字符串提供作爲輸入。這是令人沮喪的,因爲我甚至使用極其簡單的表達式(通常是通用的,直接從論壇沒有編輯)得到這個錯誤,我意識到這是由於我是PHP和DOMDocument函數的新手。php DOM空字符串提供作爲輸入錯誤

下面是一個例子函數,返回錯誤:

function breadcrumb_json() { 
    $dom = new DOMDocument(); 
    $libxml_previous_state = libxml_use_internal_errors(true); 

    // Populate $dom with $content, making sure to handle UTF-8. 
    // Also, make sure that the doctype and HTML tags are not added to our 
    // HTML fragment. http://stackoverflow.com/a/22490902/3059883 
    $dom->loadHTML($html); 
    // Create an instance of DOMXpath. 
    $xpath = new DOMXpath($dom); 

    // Get images then loop through and add additional classes. 
    $imgs = $xpath->query("//img"); 
    foreach ($imgs as $img) { 
     $existing_class = $img->getAttribute('class'); 
     echo $existing_class; 
    } 
} 

add_action('wp_footer', 'breadcrumb_json'); 

你可以從我的聰明函數名字大概看到我最終試圖爲我的網頁上的麪包屑創造JSON + LD結構化數據。底線是,無論我如何嘗試此功能,我都會收到空字符串錯誤。我的$ html變量是否需要用我的網站特定的東西替換?是否有我需要的全局變量,我錯過了?這是在Wordpress(版本4.8.2),以防萬一。

感謝您在正確的方向輕推!

回答

0

您的$html變量未定義。

PHP沒有Javascript這樣的詞彙範圍,所以你在breadcrumb_json()函數之外定義的任何東西都不是函數內部已知的。您需要將$html傳遞給breadcrumb_json($html)

+0

謝謝。這非常基本,但正是我需要的。 – TomM0419

+0

@Tom如果需要,我可以擴展它,但是您需要告訴我您希望我擴展的部分。 – Gordon