试图在循环中获取脚本时发生PHP错误


PHP error when trying to get script in a loop

我将wordpress与插件Advanced自定义字段一起使用。

我想从我的一些网站上获得一些内部文本如果我用这个php脚本做它,它可以工作

$some_link = 'http://tweakers.net/';
$tagName = 'span';
$attrName = 'class';
$attrValue = 'subtitle';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($some_link);
$html = getTags( $dom, $tagName, $attrName, $attrValue );
echo $html;
function getTags( $dom, $tagName, $attrName, $attrValue ){
    $html = '';
    $domxpath = new DOMXPath($dom);
    $newDom = new DOMDocument;
    $newDom->formatOutput = true;
   $filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
   // $filtered =  $domxpath->query('//div[@class="className"]');
   // '//' when you don't know 'absolute' path
  // since above returns DomNodeList Object
  // I use following routine to convert it to string(html); copied it from someone's post in this site. Thank you.
    $i = 0;
    while( $myItem = $filtered->item($i++) ){
        $node = $newDom->importNode( $myItem, true );    // import node
        $newDom->appendChild($node);                    // append node
    }
    $html = $newDom->saveHTML();
    return $html;
}   

只有我想在一个循环中做,这样我就可以从不同网站的中获得更多

当我把代码放成这样时:

$rows = get_field('get_attribute');
if($rows)
{
    foreach($rows as $row)
    {
       THE SAME AS ABOVE PHP CODE
    }           
}

然后我得到这个错误

  Fatal error: Call to undefined function getTags() 

当我把功能放在之上时

        $html = getTags( $dom, $tagName, $attrName, $attrValue );
        echo $html;

我得到错误的

 Fatal error: Cannot redeclare getTags() 

当我把它全部放在上面时,我什么都没有得到,没有错误,它只是空白的

我希望有人能帮助我,提前感谢

发现这是的正确方式

$rows = get_field('get_attribute');
if($rows)
{
    foreach($rows as $row)
    {
        $some_link = $row['url'];
        $tagName = $row['attribute'];
        $attrName = $row['attribute_name'];
        $attrValue = $row['attribute_value'];
        $dom = new DOMDocument;
        $dom->preserveWhiteSpace = false;
        @$dom->loadHTMLFile($some_link);
        $html = getTags( $dom, $tagName, $attrName, $attrValue );
        echo $html;
    }           
}
        function getTags( $dom, $tagName, $attrName, $attrValue ){
            $html = '';
            $domxpath = new DOMXPath($dom);
            $newDom = new DOMDocument;
            $newDom->formatOutput = true;
           $filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
           // $filtered =  $domxpath->query('//div[@class="className"]');
           // '//' when you don't know 'absolute' path
          // since above returns DomNodeList Object
          // I use following routine to convert it to string(html); copied it from someone's post in this site. Thank you.
            $i = 0;
            while( $myItem = $filtered->item($i++) ){
                $node = $newDom->importNode( $myItem, true );    // import node
                $newDom->appendChild($node);                    // append node
            }
            $html = $newDom->saveHTML();
            return $html;
        }