用 href link 替换标签
Replace a tag with href link
我有一个 html 源代码,想用它包含的 href link.[=14= 替换 all <a>
标签]
所以标签看起来像:
<a href="http://google.com" target="_blank">click here</a>
我期望输出:
http://google.com
我已经尝试过一些与 preg_replace 结合的正则表达式,
但是 none 给了我 href 内容。
那么最好的方法是什么?
匹配正则表达式 <a .*href="([^"]*)".*?<\/a>
并使用 </code> 或 <code>
.
替换为第一组
<?php
$text = '
Random text <a href="foobar.html">Foobar</a> More Text
Other text <a href="http://www.example.com">An example</a>
Still more text <a href="http://www.example.com/foo/bar.html">A deep link</a>. The end.
';
preg_match_all('/<a href="(.*?)"/i',$text,$matches);
foreach ($matches[1] as $match) {
print "A link: $match\n";
}
结果:
A link: foobar.html
A link: http://www.example.com
A link: http://www.example.com/foo/bar.html
我有一个 html 源代码,想用它包含的 href link.[=14= 替换 all <a>
标签]
所以标签看起来像:
<a href="http://google.com" target="_blank">click here</a>
我期望输出:
http://google.com
我已经尝试过一些与 preg_replace 结合的正则表达式, 但是 none 给了我 href 内容。
那么最好的方法是什么?
匹配正则表达式 <a .*href="([^"]*)".*?<\/a>
并使用 </code> 或 <code>
.
<?php
$text = '
Random text <a href="foobar.html">Foobar</a> More Text
Other text <a href="http://www.example.com">An example</a>
Still more text <a href="http://www.example.com/foo/bar.html">A deep link</a>. The end.
';
preg_match_all('/<a href="(.*?)"/i',$text,$matches);
foreach ($matches[1] as $match) {
print "A link: $match\n";
}
结果:
A link: foobar.html
A link: http://www.example.com
A link: http://www.example.com/foo/bar.html