How to reset the base tag

1.4k Views Asked by At

So am using simple html dom , i want to retrieve part of the page, that seems to work fine :) but the links are not correct , they are relative to my site... on which i decided to use the base tag instead ....here is what i have

<?php
include('simple_html_dom.php');

$url = "http://bm.erciyes.edu.tr/";
$file = file_get_html($url);

echo "<base href='$url'>";
foreach($file->find('div.onemliduyurular') as $var){
    echo $var->innertext;
}


$url = "http://bm.erciyes.edu.tr/";
?>

<h1>Return to my Site</h1>
<?php

// I want this link here become relative to my site again
echo "<a href='hello.php'>Go This Way</a>"; 

so added this line above the foreach

echo "<base href='$url'>";

is there a way in which i can reset the base tag? or any other alternative ;)

inner text contains this

// get dom node's inner html
    function innertext()
    {
        if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];
        if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);

        $ret = '';
        foreach ($this->nodes as $n)
            $ret .= $n->outertext();
        return $ret;
    }

and this is the announcement am pulling

2

There are 2 best solutions below

0
On BEST ANSWER

One of the way is done by using absolute url

... What if you prepend your url with the relative one that comes from the pulled site....
As in

<?php
foreach($html->find('div.onemliduyurular') as $d) {
    foreach($d->find('a[href]') as $goAway){
        $goAway->href =$url.$goAway->href;
        }
    echo $d->innertext;
}
$html->clear();
unset($html);

I hope it may work with your project

5
On

You cannot change the base address in another base element, since the HTML syntax for head allows at most one base element. HTML5 does not change this; it explicitly says: “If there are multiple base elements with href attributes, all but the first are ignored.”

The conclusion is that you need to emit appropriate absolute URLs.