dongshan9338 2011-06-20 13:24 采纳率: 0%
浏览 12
已采纳

php将文本插入到href中

I'm working on using htmlpurifier to create a text-only version of my site. I now need to replace all the a hrefs with the text only url i.e. 'www.example.com/aboutus' becomes 'www.example.com/text/aboutus'

Initially I tried a simple str_replace on the domain (I use a global variable for the domain), but the problem is links to files also get replaced i.e. 'www.example.com/document.pdf' becomes 'www.example.com/text/document.pdf' and therefore fails.

Is there a regular expression where I can say replace domain with domain/text where the url does not include string?

Thanks for any pointers you might be able to give me :)

  • 写回答

1条回答 默认 最新

  • duanshan2988 2011-06-20 13:41
    关注

    Use a negative lookahead:

    $output = preg_replace(
                 '#www.example.com(?!/text/)#', 
                 'www.example.com/text', 
                 $input
              );
    

    Better yet, use DOM with it:

    $html = '<a href="www.example.com/something">foo</a>
             <p>hello</p>
             <a href="www.example.com/text/documents">bar</a>';
    
    libxml_use_internal_errors(true); // supresses DOM errors
    
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    
    $hrefs = $xpath->query('//a/@href');
    foreach ($hrefs as $href) {
        $href->value = preg_replace(
                          '#^www.example.com(?!/text/)(.*?)(?<!\.pdf)$#', 
                          'www.example.com/text\\1', 
                          $href->value
                       );
    }
    

    This should give you:

    <a href="www.example.com/text/something">foo</a>
    <p>hello</p>
    <a href="www.example.com/text/documents">bar</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解