7

次のコードを使用して、別のページから html を取得し、php ページに配置しています。

$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">

4

1 に答える 1

12

次のようなものを試してください:

$xml = new DOMDocument(); 
$xml->loadHTMLFile($url); 
foreach($xml->getElementsByTagName('a') as $link) { 
   $oldLink = $link->getAttribute("href");
   $link->setAttribute('href', "http://mydomain.com/" . $oldLink);
}
echo $xml->saveHtml();
于 2013-03-18T03:32:36.260 に答える