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';