Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法通过PHP解析页面中的链接(href)_Php - Fatal编程技术网

无法通过PHP解析页面中的链接(href)

无法通过PHP解析页面中的链接(href),php,Php,请看下面我的脚本: <?php function getContent () { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, 'http://localhost/test.php/test2.php'); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $output=curl_exec($ch);

请看下面我的脚本:

<?php

    function getContent ()
    {
        $ch = curl_init();  
        curl_setopt($ch,CURLOPT_URL, 'http://localhost/test.php/test2.php');
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $output=curl_exec($ch);
        curl_close($ch);
        return $output;

    }

    function getHrefFromLinks ($cString){

        libxml_use_internal_errors(true);

        $dom = new DomDocument();
        $dom->loadHTML($cString);

        $xpath = new DOMXPath($dom);
        $nodes = $xpath->query('//a/@href');
        foreach($nodes as $href) {

            echo $href->nodeValue;   echo "<br />";                    // echo current attribute value
            $href->nodeValue = 'new value';              // set new attribute value
            $href->parentNode->removeAttribute('href');  // remove attribute
        }

        foreach (libxml_get_errors() as $error) {

        }

        libxml_clear_errors();

    }



echo getHrefFromLinks (getContent());

?>

其输出为:


回显gethrefromlinks(getContent())时运行时,输出为:

/oncelink/index.html<br />/oncelink-2/lucky<br />
/oncelink/index.html
/oncelink-2/lucky
这是错误的,因为输出应该是:

/oncelink/index.html<br />/oncelink-2/lucky'locki<br />
/oncelink/index.html
/oncelink-2/lucky'locki
我知道从链接生成的href值在某种程度上是不正确的,因为它包含了一个额外的撇号,但我无法更改它,因为它是预生成的

另一个问题是,如何获取span标记的值:

<span class="lsbold">

提前谢谢

已解决:)

嗯。如果它是愚蠢的,但它起作用,那么它就不愚蠢:D

最后添加了以下代码:

$fix = str_replace("href='", 'href="', getContent());
$fix = str_replace("'>", '">', $fix);
echo getHrefFromLinks ($fix);
已解决:)

嗯。如果它是愚蠢的,但它起作用,那么它就不愚蠢:D

最后添加了以下代码:

$fix = str_replace("href='", 'href="', getContent());
$fix = str_replace("'>", '">', $fix);
echo getHrefFromLinks ($fix);

当您首先向DOM解析器中输入无效的HTML时,您不能期望它得到您想要的结果。您的第二个链接在任何现代浏览器中也只能链接到
/oncelink-2/lucky
,并按照规定进行错误更正。感谢您的评论@CBroe,您能推荐一个替代方法吗?首先是谁生成了损坏的HTML,为什么不能要求他们修复此问题?跳出圈套以某种方式尝试和处理混乱的数据总是比实际修复数据更不可取的选择…@CBroe输出是从一个我们无法访问的网站生成的,而我们目前没有管理员权限来检查它。由于我们需要完成这项工作,我们正在考虑使用正则表达式,但似乎不能让它工作。当您开始向DOM解析器中输入无效的HTML时,您不能期望DOM解析器得到您想要的结果。您的第二个链接在任何现代浏览器中也只能链接到
/oncelink-2/lucky
,并按照规定进行错误更正。感谢您的评论@CBroe,您能推荐一个替代方法吗?首先是谁生成了损坏的HTML,为什么不能要求他们修复此问题?跳出圈套以某种方式尝试和处理混乱的数据总是比实际修复数据更不可取的选择…@CBroe输出是从一个我们无法访问的网站生成的,而我们目前没有管理员权限来检查它。由于我们需要完成这项工作,我们正在考虑使用正则表达式,但似乎不能让它工作。