PHP preg_replace: 用正则表达式替换文本中的所有锚标记及其 href 值
PHP preg_replace: Replace all anchor tags in text with their href value with Regex
我想用 href 值替换文本中的所有锚标记,但我的模式无法正常工作。
$str = 'This is a text with multiple anchor tags. This is the first one: <a href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.';
$test = preg_replace("/<a\s.+href=['|\"]([^\"\']*)['|\"].*>[^<]*<\/a>/i",'', $str);
echo $test;
最后的文字应该是这样的:
This is a text with multiple anchor tags. This is the first one: https://www.link1.com/ and this one the second: https://www.link2.com/ after that a lot of other text. And here the 3rd one: https://www.link3.com/ Some other text.
非常感谢!
可能不是更简单,但更安全的是使用 strpos 循环字符串以查找和剪切字符串并删除 html。
$str = 'This is a text with multiple anchor tags. This is the first one: <a class="funky-style" href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.';
$pos = strpos($str, '<a');
while($pos !== false){
// Find start of html and remove up to link (<a href=")
$str = substr($str, 0, $pos) . substr($str, strpos($str, 'href="', $pos)+6);
// Find end of link and remove that.(" title="Link 1">Link 1</a>)
$str = substr($str, 0, strpos($str,'"', $pos)) . substr($str, strpos($str, '</a>', $pos)+4);
// Find next link if possible
$pos = strpos($str, '<a');
}
echo $str;
编辑以处理 a 标签的不同顺序。
如果您仍然使用正则表达式,这应该有效:
preg_replace("/<a\s+href=['\"]([^'\"]+)['\"][^\>]*>[^<]+<\/a>/i",'', $str);
但是您最好使用 Andreas 发布的解决方案。
仅供参考:您之前的正则表达式不起作用的原因是这个小数字:
.*>
因为 .
select 所有你最终匹配的所有超过 url 的东西都被替换;一路走到尽头。这就是为什么它似乎只 select 并替换它找到的第一个锚标记并切断其余部分的原因。
将其更改为
[^\>]*
确保此特定 selection 仅限于存在于 url 和 a 标记的结束括号之间的字符串部分。
就是不要。
改用解析器。
$dom = new DOMDocument();
// since you have a fragment, wrap it in a <body>
$dom->loadHTML("<body>".$str."</body>");
$links = $dom->getElementsByTagName("a");
while($link = $links[0]) {
$link->parentNode->insertBefore(new DOMText($link->getAttribute("href")),$link);
$link->parentNode->removeChild($link);
}
$result = $dom->saveHTML($dom->getElementsByTagName("body")[0]);
// remove <body>..</body> wrapper
$output = substr($result, strlen("<body>"), -strlen("</body>"));
如果您想用 href 值替换标签,您可以这样做:
$post = preg_replace("/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/","",$post);
如果要替换为文本值:
$post = preg_replace("/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/","",$post);
我想用 href 值替换文本中的所有锚标记,但我的模式无法正常工作。
$str = 'This is a text with multiple anchor tags. This is the first one: <a href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.';
$test = preg_replace("/<a\s.+href=['|\"]([^\"\']*)['|\"].*>[^<]*<\/a>/i",'', $str);
echo $test;
最后的文字应该是这样的:
This is a text with multiple anchor tags. This is the first one: https://www.link1.com/ and this one the second: https://www.link2.com/ after that a lot of other text. And here the 3rd one: https://www.link3.com/ Some other text.
非常感谢!
可能不是更简单,但更安全的是使用 strpos 循环字符串以查找和剪切字符串并删除 html。
$str = 'This is a text with multiple anchor tags. This is the first one: <a class="funky-style" href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.';
$pos = strpos($str, '<a');
while($pos !== false){
// Find start of html and remove up to link (<a href=")
$str = substr($str, 0, $pos) . substr($str, strpos($str, 'href="', $pos)+6);
// Find end of link and remove that.(" title="Link 1">Link 1</a>)
$str = substr($str, 0, strpos($str,'"', $pos)) . substr($str, strpos($str, '</a>', $pos)+4);
// Find next link if possible
$pos = strpos($str, '<a');
}
echo $str;
编辑以处理 a 标签的不同顺序。
如果您仍然使用正则表达式,这应该有效:
preg_replace("/<a\s+href=['\"]([^'\"]+)['\"][^\>]*>[^<]+<\/a>/i",'', $str);
但是您最好使用 Andreas 发布的解决方案。
仅供参考:您之前的正则表达式不起作用的原因是这个小数字:
.*>
因为 .
select 所有你最终匹配的所有超过 url 的东西都被替换;一路走到尽头。这就是为什么它似乎只 select 并替换它找到的第一个锚标记并切断其余部分的原因。
将其更改为
[^\>]*
确保此特定 selection 仅限于存在于 url 和 a 标记的结束括号之间的字符串部分。
就是不要。
改用解析器。
$dom = new DOMDocument();
// since you have a fragment, wrap it in a <body>
$dom->loadHTML("<body>".$str."</body>");
$links = $dom->getElementsByTagName("a");
while($link = $links[0]) {
$link->parentNode->insertBefore(new DOMText($link->getAttribute("href")),$link);
$link->parentNode->removeChild($link);
}
$result = $dom->saveHTML($dom->getElementsByTagName("body")[0]);
// remove <body>..</body> wrapper
$output = substr($result, strlen("<body>"), -strlen("</body>"));
如果您想用 href 值替换标签,您可以这样做:
$post = preg_replace("/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/","",$post);
如果要替换为文本值:
$post = preg_replace("/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/","",$post);