查找并追加某个类的3个参数


Find and append hrefs of a certain class

我一直在寻找解决方案,但还没有找到完全正确的东西。

情况是这样的:我需要找到具有给定类(例如class="tracker")的页面上的所有链接,然后在末尾附加查询字符串值,因此当用户加载页面时,这些特定链接将使用一些动态信息更新。

我知道如何用Javascript来完成,但我真的很想适应它来运行服务器端。我对PHP很陌生,但是从它的外观来看,XPath可能是我正在寻找的,但是我还没有找到一个合适的例子来开始。有类似GetElementByClass的东西吗?

任何帮助都将非常感激!

Shadowise

有像GetElementByClass这样的东西吗?

这是我的一个实现…

function getElementsByClassName(DOMDocument $domNode, $className) {
    $elements = $domNode->getElementsByTagName('*');
    $matches = array();
    foreach($elements as $element) {
        if ( ! $element->hasAttribute('class')) {
            continue;
        }
        $classes = preg_split('/'s+/', $element->getAttribute('class'));
        if ( ! in_array($className, $classes)) {
            continue;
        }
        $matches[] = $element;
    }
    return $matches;
}

这个版本不依赖于上面的helper函数。

$str = '<body>
    <a href="">a</a>
        <a href="http://example.com" class="tracker">a</a>
        <a href="http://example.com?hello" class="tracker">a</a>
    <a href="">a</a>
</body>
    ';
$dom = new DOMDocument;
$dom->loadHTML($str);
$anchors = $dom->getElementsByTagName('body')->item(0)->getElementsByTagName('a');
foreach($anchors as $anchor) {
    if ( ! $anchor->hasAttribute('class')) {
        continue;
    }
    $classes = preg_split('/'s+/', $anchor->getAttribute('class'));
    if ( ! in_array('tracker', $classes)) {
        continue;
    }
    $href = $anchor->getAttribute('href');
    $url = parse_url($href);
    $attach = 'stackoverflow=true';
    if (isset($url['query'])) {
        $href .= '&' . $attach;
    } else {
        $href .= '?' . $attach;
    }
    $anchor->setAttribute('href', $href);
}
echo $dom->saveHTML();

输出
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <a href="">a</a>
        <a href="http://example.com?stackoverflow=true" class="tracker">a</a>
        <a href="http://example.com?hello&amp;stackoverflow=true" class="tracker">a</a>
    <a href="">a</a>
</body></html>

我需要找到一个页面上的所有链接对于给定的类(例如class="tracker")[…]我对PHP很陌生,但是从看起来,XPath可能就是我想要的我一直在找,但还没有找到适合作为开始的例子。有没有类似的GetElementByClass ?

XPath 1.0表达式:

//a[contains(
       concat(' ',normalize-space(@class),' '),
       ' tracker '
    )
]

稍微短一点,使用xpath:

$dom = new DomDocument();
$dom->loadXml('<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <a href="somlink" class="tracker foo">label</a>
    <a href="somlink" class="foo">label</a>
    <a href="somlink">label</a>
    <a href="somlink" class="atrackerb">label</a>
    <a href="somlink">label</a>
    <a href="somlink" class="tracker">label</a>
    <a href="somlink" class="tracker">label</a>
</root>');
$xpath = new DomXPath($dom);
foreach ($xpath->query('//a[contains(@class, "tracker")]') as $node) {
    if (preg_match('/'btracker'b/', $node->getAttribute('class'))) {
        $node->setAttribute(
            'href',
            $node->getAttribute('href') . '#some_extra'
        );
    }
}
header('Content-Type: text/xml; charset"UTF-8"');
echo $dom->saveXml();