Kei*_*gan 1 php regex str-replace
说我有以下链接:
<li class="hook">
<a href="i_have_underscores">I_have_underscores</a>
</li>
Run Code Online (Sandbox Code Playgroud)
我怎么样,只删除文本中的下划线而不是href?我使用了str_replace,但这删除了所有下划线,这并不理想.
所以基本上我会留下这个输出:
<li class="hook">
<a href="i_have_underscores">I have underscores</a>
</li>
Run Code Online (Sandbox Code Playgroud)
任何帮助,非常感谢
您可以使用HTML DOM解析器获取标记内的文本,然后str_replace()在结果上运行您的函数.
使用我链接的DOM Parser,它就像这样简单:
$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>
Run Code Online (Sandbox Code Playgroud)
而已.