[英]How to replace the urls into clickable links except anchor text & anchor href?
[英]How to copy the anchor text and replace the href with using PHP
您好,我有大约1000个链接的锚文本格式如下
<a href="1_1_3.html" >my anchor text1</a>
<a href="1_4_8.html" >my anchor text2</a>
....等
我想用自己的锚文本替换所有href链接,并在网址末尾添加.php
结果必须是这样的:
<a href="my anchor text1.php" >my anchor text1</a>
<a href="my anchor text2.php" >my anchor text2</a>
在此先感谢您的时间。
$dom = new DOMDocument();
$dom->loadHTML($yourhtml);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $a) {
$href = $a->getAttribute('href');
.... manipulate the href
$a->setAttribute('href', $href);
}
$newhtml = $dom->saveHTML();
$string = '<a href="1_1_3.html" >my anchor text1</a>
<a href="1_4_8.html" >my anchor text2</a>';
$string = preg_replace('#<a href="(.*?)" >(.*?)</a>#is', '<a href="\\2.php" >\\2</a>', $string);
echo $string;
结果:
<a href="my anchor text1.php" >my anchor text1</a>
<a href="my anchor text2.php" >my anchor text2</a>
演示: http : //ideone.com/k9NOc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.