Php Str_替换为regex

Php Str_替换为regex,php,regex,str-replace,Php,Regex,Str Replace,假设我有以下链接: <li class="hook"> <a href="i_have_underscores">I_have_underscores</a> </li> 如何删除文本中的下划线而不是href?我使用了str_replace,但这会删除所有下划线,这并不理想 所以基本上我只剩下这个输出: <li class="hook"> <a href="i_have_underscores"&g

假设我有以下链接:

<li class="hook">
      <a href="i_have_underscores">I_have_underscores</a>
</li>
  • 如何删除文本中的下划线而不是href?我使用了str_replace,但这会删除所有下划线,这并不理想

    所以基本上我只剩下这个输出:

    <li class="hook">
          <a href="i_have_underscores">I have underscores</a>
    </li>
    
  • 非常感谢您提供的任何帮助

    您可以使用获取标记中的文本,然后对结果运行
    str_replace()
    函数


    使用我链接的DOM解析器,它非常简单,如下所示:

    $html = str_get_html(
        '<li class="hook"><a href="i_have_underscores">I_have_underscores</a></li>');
    $links = $html->find('a');   // You can use any css style selectors here
    
    foreach($links as $l) {
        $l->innertext = str_replace('_', ' ', $l->innertext)
    }
    
    echo $html
    //<li class="hook"><a href="i_have_underscores">I have underscores</a></li>
    
    $html=str\u get\u html(
    “
  • ”; $links=$html->find('a');//您可以在此处使用任何css样式选择器 foreach($l链接){ $l->innertext=str\u replace(“”,“”,$l->innertext) } echo$html //
  • 就是这样。

    您可以使用获取标记中的文本,然后对结果运行
    str\u replace()
    函数


    使用我链接的DOM解析器,它非常简单,如下所示:

    $html = str_get_html(
        '<li class="hook"><a href="i_have_underscores">I_have_underscores</a></li>');
    $links = $html->find('a');   // You can use any css style selectors here
    
    foreach($links as $l) {
        $l->innertext = str_replace('_', ' ', $l->innertext)
    }
    
    echo $html
    //<li class="hook"><a href="i_have_underscores">I have underscores</a></li>
    
    $html=str\u get\u html(
    “
  • ”; $links=$html->find('a');//您可以在此处使用任何css样式选择器 foreach($l链接){ $l->innertext=str\u replace(“”,“”,$l->innertext) } echo$html //

  • 就是这样。

    用而不是正则表达式解析HTML更安全。请尝试以下代码:

    <?php
    
    function replaceInAnchors($html)
    {
        $dom = new DOMDocument();
        // loadHtml() needs mb_convert_encoding() to work well with UTF-8 encoding
        $dom->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"));
    
        $xpath = new DOMXPath($dom);
    
        foreach($xpath->query('//text()[(ancestor::a)]') as $node)
        {
            $replaced = str_ireplace('_', ' ', $node->wholeText);
            $newNode  = $dom->createDocumentFragment();
            $newNode->appendXML($replaced);
            $node->parentNode->replaceChild($newNode, $node);
        }
    
        // get only the body tag with its contents, then trim the body tag itself to get only the original content
        return mb_substr($dom->saveXML($xpath->query('//body')->item(0)), 6, -7, "UTF-8");
    }
    
    $html = '<li class="hook">
          <a href="i_have_underscores">I_have_underscores</a>
    </li>';
    echo replaceInAnchors($html);
    
    parentNode->replaceChild($newNode,$node);
    }
    //仅获取包含其内容的body标记,然后修剪body标记本身以仅获取原始内容
    返回mb_substr($dom->saveXML($xpath->query('//body')->item(0)),6,-7,“UTF-8”);
    }
    $html='
  • '; echo replaceinachors($html);
    使用而不是正则表达式解析HTML更安全。请尝试以下代码:

    <?php
    
    function replaceInAnchors($html)
    {
        $dom = new DOMDocument();
        // loadHtml() needs mb_convert_encoding() to work well with UTF-8 encoding
        $dom->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"));
    
        $xpath = new DOMXPath($dom);
    
        foreach($xpath->query('//text()[(ancestor::a)]') as $node)
        {
            $replaced = str_ireplace('_', ' ', $node->wholeText);
            $newNode  = $dom->createDocumentFragment();
            $newNode->appendXML($replaced);
            $node->parentNode->replaceChild($newNode, $node);
        }
    
        // get only the body tag with its contents, then trim the body tag itself to get only the original content
        return mb_substr($dom->saveXML($xpath->query('//body')->item(0)), 6, -7, "UTF-8");
    }
    
    $html = '<li class="hook">
          <a href="i_have_underscores">I_have_underscores</a>
    </li>';
    echo replaceInAnchors($html);
    
    parentNode->replaceChild($newNode,$node);
    }
    //仅获取包含其内容的body标记,然后修剪body标记本身以仅获取原始内容
    返回mb_substr($dom->saveXML($xpath->query('//body')->item(0)),6,-7,“UTF-8”);
    }
    $html='
  • '; echo replaceinachors($html);
    谢谢,我应该查看网站的哪一部分?在首页上,你会想查看“下载和文档”下的两个链接,它比stillstanding的解决方案慢得多(在我的机器上是30ms,而在我的机器上是1ms),这对我来说似乎是最好的方法(但是使用
    “//text()[(祖先::a)]”
    xPath查询)。谢谢,我应该查看站点的哪一部分?在首页上,你会想查看“下载和文档”下的两个链接,它比stillstanding的解决方案慢得多(在我的机器上是30ms,而在我看来这是最好的解决方案,但是使用
    “//text()[(祖先::a)]”
    xPath查询)。(相关)(相关)