0

我试图将 div 的 innerhtml 连接到字符串变量中:
游戏变量:

$games = '';


DOMinnerHTML 函数:

function DOMinnerHTML($element) 
{ 
    $innerHTML = ""; 
    $children = $element->childNodes; 
    foreach ($children as $child) 
    { 
        $tmp_dom = new DOMDocument(); 
        $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
        $innerHTML.=trim($tmp_dom->saveHTML()); 
    } 
    return $innerHTML; 
} 


ExtractFromType 函数:

function ExtractFromType($type)
{
$html = file_get_contents('www.site.com/' .$type);
$dom = new domDocument;
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {
if (strpos($div->getAttribute('style'),'MyString') !== false) {

//////
$games = $games.DOMinnerHTML($div);
//////

}
}
}


代码:

ExtractFromType('MyType');
echo $games; // = Nothing.

此代码不返回任何内容。

4

1 回答 1

0

$games在全局范围内定义,在ExctractFromType. 在函数内部定义它,然后返回值:

function ExtractFromType($type) {
  $html = file_get_contents('www.site.com/' .$type);
  $dom = new domDocument;
  @$dom->loadHTML($html);
  $dom->preserveWhiteSpace = false;
  $divs = $dom->getElementsByTagName('div');

  $games = '';
  foreach ($divs as $div) {
    if (strpos($div->getAttribute('style'),'MyString') !== false) {
      $games = $games.DOMinnerHTML($div);
    }
  }
}

echo ExtractFromType('MyType');
于 2013-01-15T20:20:26.563 回答