Php 将锚定标记替换为括号中的href

Php 将锚定标记替换为括号中的href,php,html,Php,Html,我想用纯文本的href部分替换html锚定标记 所以 $input=“请转到或并搜索。”; 回声固定链接($输入); //“请转到谷歌(http://www.google.com)还是雅虎(http://www.yahoo.com)和搜索。” 更新:我可以使用正则表达式来完成这项工作,但它还需要对输入字符串中的许多URL起作用 更新:最后做了这件事,下面是Elmo答案的一个变体: preg_replace("/<a.+href=['|\"]([^\"\']*)['|\"].*>(.

我想用纯文本的href部分替换html锚定标记

所以

$input=“请转到或并搜索。”;
回声固定链接($输入);
//“请转到谷歌(http://www.google.com)还是雅虎(http://www.yahoo.com)和搜索。”
更新:我可以使用正则表达式来完成这项工作,但它还需要对输入字符串中的许多URL起作用

更新:最后做了这件事,下面是Elmo答案的一个变体:

preg_replace("/<a.+href=['|\"]([^\"\']*)['|\"].*>(.+)<\/a>/i",'\2 (\1)',$html)

preg\u replace(“/您可以将所有HTML重写为:

<a href='http://www.google.com'>Google</a> (http://www.google.com)
(http://www.google.com)
或者,您可以使用javascript自动执行此操作,页面中的所有
标记:

var theLinks=document.querySelectorAll('a');
for (var x=0; x<theLinks.length; x++){
  theLinks[x].outerHTML+=" ("+theLinks[x].href+")";
}
var theLinks=document.querySelectorAll('a');

对于(var x=0;x,可以在PHP中使用正则表达式:

$input_string = "Please go to <a href='http://www.google.com' style='color:red'>Google</a> and search.";
$pattern = "/<a[\w\s\.]*href='([\w:\-\/\.]*)'[\w\s\.\=':>]+<\/a>/";
$replacement = '(\1)';
$output_string = preg_replace($pattern, $replacement, $input_string);
$input\u string=“请转到并搜索。”;

$pattern=“/这里有一个更通用的版本(这个版本更短,更可靠):


preg\u replace('/

您尝试了什么?@danielb我已经更新了答案,现在它支持多个锚。不过,感谢似乎不起作用。它返回“请转到()并搜索”但是它经过测试-你必须拼错一些东西哦!对不起-错误在第三行-双引号导致将``作为转义字符。这里应该使用单引号。我最后使用了这个,你写的一个变体。你认为呢?preg_replace(“/
$input_string = "Please go to <a href='http://www.google.com' style='color:red'>Google</a> and search.";
$pattern = "/<a[\w\s\.]*href='([\w:\-\/\.]*)'[\w\s\.\=':>]+<\/a>/";
$replacement = '(\1)';
$output_string = preg_replace($pattern, $replacement, $input_string);