我正在寻找一种方式来改造这个:更换HTML标记的HREF
...[inner content]...
进入这个:
...[inner content]...
的情况下有多个链接与其他showinfo:整数]值。 (我可以处理这些的)
感谢您的帮助, 巴林特
编辑:多亏了凯撒的答案,这里是工作的代码片段:
$html = $a;
$dom = new \DOMDocument;
@$dom->loadHTML($html); //Cannot guarantee all-valid input
foreach ($dom->getElementsByTagName('a') as $tag) {
// Fixed strstr order and added a != false check - the, because the string started with the substring
if ($tag->hasAttribute('href') && strstr($tag->getAttribute('href'), 'showinfo:3875') != false) {
$tag->setAttribute('href', "http://somelink.com/{$tag->textContent}");
// Assign the Converted HTML, prevents failing when saving
$html = $tag;
}
}
return $dom->saveHTML($dom);
}
2015-01-17
molbal