我使用下面的代码从另一个页面抓取HTML,并把它变成我的PHP页面:PHP的DomDocument编辑各个环节
$doc = new DomDocument;
// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('{URL IS HERE}'));
$content = $doc->getElementById('form2');
echo $doc->SaveHTML($content);
我想改变的<a href="/somepath/file.htm">
所有实例,这样我可以预先考虑到它实际的域名。我怎样才能做到这一点?
因此,它需要将它们改为:<a href="http://mydomain.com/somepath/file.htm">
。
如果我是你,我会尽量避免使用'DomDocument'并直接使用正则表达式来找出链接和编辑。 – Raptor 2013-03-18 03:23:38
怎么回事?无处不在我堆栈溢出,他们说你应该使用'DomDocument'。你能给我一个如何用正则表达式来做这个例子吗? – 2013-03-18 03:25:33
您可以为查找和替换任务创建额外的对象。额外解析时间和内存花费。尝试:http://stackoverflow.com/questions/4001328/php-regex-to-get-string-inside-href-tag – Raptor 2013-03-18 03:29:14