【问题标题】:Adding attributes to submitted content links向提交的内容链接添加属性
【发布时间】:2009-09-23 18:05:43
【问题描述】:

我有一个网站供人们提交文本。我想为添加的链接添加几个属性。有人知道将 rel="nofollow" 添加到提交的文本正文中的任何链接的正则表达式(或其他好方法)吗?

【问题讨论】:

    标签: php html regex


    【解决方案1】:

    使用 DOM 解析(PHP Simple HTML DOM Parser 示例):

    // Create DOM from string
    $html = str_get_html($links);
    
    $linksCount = count($html->find('a'));
    
    for($i=0;$i<$linksCount;$i++) {
        $html->find('a', $i)->rel = 'nofollow';
    }
    echo $html;
    

    【讨论】:

    • 谢谢。这看起来像是一个很好的下降解决方案。
    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多