查找并添加某一类的HREF [英] Find and append hrefs of a certain class

查看:138
本文介绍了查找并添加某一类的HREF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个解决方案,这一点,但还没有找到很正确的事情呢。

的情况是这样的:
我需要找到一个给定的类网页上的所有链接(比如类=跟踪器),然后在年底追加查询字符串值,所以当用户加载页面,这些某些环节与一些动态信息进行更新。

我知道这是可以做到的的JavaScript ,但我真的很想去适应它运行服务器端来代替。我是很新的 PHP ,但是从外观上来看,的XPath 可能是什么我要找的,但我还没有找到一个合适的例子上手用。有没有像 GetElementByClass

什么

任何帮助将大大AP preciated!

Shadowise


解决方案

  

有没有像 GetElementByClass

什么

下面是我掀起了一个实现...

 函数getElementsByClassName(DOM文档$れ,$的className){
    $元素= $れ>的getElementsByTagName('*');
    $匹配=阵列();
    的foreach($元素作为$元素){
        如果($元素 - >!hasAttribute(类)){
            继续;
        }
        $类= preg_split('/ \\ s + /',$&元素 - GT;的getAttribute(类));
        如果(!in_array($类名,$类)){
            继续;
        }
        $匹配[] = $元素;
    }
    返回$匹配;
}

该版本不依赖于上面的辅助函数。

  $海峡='<身体GT;
    &所述; A HREF =>一种与所述; / A>
        < A HREF =htt​​p://example.com级=跟踪器>将< / A>
        < A HREF =htt​​p://example.com?hello级=跟踪器>将< / A>
    &所述; A HREF =>一种与所述; / A>
< /身体GT;
    ';$ DOM =新的DOM文档;$ dom-> loadHTML($海峡);$锚= $ dom->的getElementsByTagName('身体') - GT;项目(0) - >的getElementsByTagName('A');的foreach($作为锚锚$){    如果($锚固>!hasAttribute(类)){
        继续;
    }    $类= preg_split('/ \\ s + /',$锚固>的getAttribute(类));    如果(!in_array('跟踪',$类)){
        继续;
    }    $ HREF = $锚固>的getAttribute(href属性);    $ URL = parse_url($ HREF);    $连接='计算器=真;    如果(使用isset($网址['查询'])){
        $ HREF ='和;。 。 $连接;
    }其他{
        $ HREF =? 。 $连接;
    }    $锚固>的setAttribute('href属性,$ HREF);
}回声$ dom-> saveHTML();

输出

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.0过渡// ENhttp://www.w3.org/TR/REC-html40/ loose.dtd>
< HTML和GT;<身体GT;
    &所述; A HREF =>一种与所述; / A>
        < A HREF =htt​​p://example.com?stackoverflow=true级=跟踪器>将< / A>
        < A HREF =htt​​p://example.com?hello&stackoverflow=true级=跟踪器>将< / A>
    &所述; A HREF =>一种与所述; / A>
< /身体GT;< / HTML>

I've been searching for a solution to this but haven't found quite the right thing yet.

The situation is this: I need to find all links on a page with a given class (say class="tracker") and then append query string values on the end, so when a user loads a page, those certain links are updated with some dynamic information.

I know how this can be done with Javascript, but I'd really like to adapt it to run server side instead. I'm quite new to PHP, but from the looks of it, XPath might be what I'm looking for but I haven't found a suitable example to get started with. Is there anything like GetElementByClass?

Any help would be greatly appreciated!

Shadowise

解决方案

Is there anything like GetElementByClass?

Here is an implementation I whipped up...

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

This version doesn't rely on the helper function above.

$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();

Output

<!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>

这篇关于查找并添加某一类的HREF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆