尝试在循环中获取脚本时出现PHP错误

时间:2015-02-14 11:19:40

标签: php domxpath

我正在使用wordpress与插件高级自定义字段。

我想从我的一些网站获得一些内部文字 如果我使用这个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() 

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

我希望有人可以帮助我,提前致谢

1 个答案:

答案 0 :(得分:0)

发现这是正确的方法

$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;
        }