html链接中的href的Preg替换/Preg匹配项? [英] Preg Replace / Preg Match for href in html link?
本文介绍了html链接中的href的Preg替换/Preg匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都有一个正则表达式来替换以下代码:
Anybody have a regular expression to replace the following code:
<a href="originalLink">hi</a>
具有:
<a href="newLink">hi</a>
推荐答案
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all links
$anchors = $html->find('a');
$count = count($anchors);
for($i=0;$i<$count;$i++) {
$anchors[$i]->href = 'someLink.html';
}
如果知道要替换的锚点的href
,请执行以下操作:
If you know the href
of the anchor you want to replace, do something like:
$html->find('a[href=something]', 0)->href = 'someLink.php';
这篇关于html链接中的href的Preg替换/Preg匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文