change all href of external page after insert it

- 1 answer

Ad

I have inserted external content to my page using simple_html_dom.php but all href not working for example :

 href='oldpage.php'

and I want this like to be changed to

href='http://localhost/oldpage.php'

or

href='http://localhost/mynewpage.php'

is there a way to change all href to new one like add some string before it ? Thank you

Ad

Answer

Ad

You could do a simple string replace like:

str_replace('target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="', 'target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="http://www.example.com/', $string);

or with domdocument:

$page = '<html><head></head><body><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="simple"></a><h1>Hi</h1><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="simple2"></a></body></html>';
$doc = new DOMDocument();
$doc->loadHTML($page);
$as = $doc->getElementsByTagName('a');
foreach($as as $a){
    $a->setAttribute('href', 'http://www.example.com/' . $a->getAttribute('href'));
}
print_r($doc->saveHTML());

output:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head></head><body><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="http://www.example.com/simple"></a><h1>Hi</h1><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="http://www.example.com/simple2"></a></body></html>

This doesn't take into account absolute paths, you'll need a regex approach for that..

If the quote types vary you also will need to use a regex for the str_replace example. Can do something like('|") for that then use $1 to match the quote type.

Ad
source: stackoverflow.com
Ad