使用href链接并使用正则表达式将其添加到变量中

时间:2012-09-06 02:40:52

标签: php regex href

我是php和regex的新手。

我有来自旧应用的HTML代码:

$oldlinkshtml = "<li class="active"><a href="www.example.com?page=1">Previous</a></li>";
// desired output
$oldlinksvar = "";

echo $oldlinksvar;

我想删除$ oldlinkshtml中的所有html标记,只取“www.example.com?page=1”并将其添加到$ oldlinksvar。我怎么能用正则表达式做到这一点?请帮帮我。

1 个答案:

答案 0 :(得分:0)

preg_replace('/<a(.*?)href="([^"]*)"(.*?)>/','<a $1 href="http://new.href/?old_url=$2" $3>',$text);


输入:<li class="active"><a href="www.example.com?page=1">Previous</a></li>
输出:<li class="active"><a href="http://new.href/?old_url=www.example.com?page=1" >Previous</a></li>