test

Change relative URL to absolute URL

for example, I have a line like this:

$html = ' <a href="test.html">test</a> <a href="http://mydomain.com/test.html">test</a> <a href="http://otherdomain.com/test.html">test</a> <a href="someothertest/otherdir/hi.html">hi</a> '; 

and I want to add an absolute URL to all hrefs where no domain is specified.

 $html = ' <a href="http://mydomain.com/test.html">test</a> <a href="http://mydomain.com/test.html">test</a> <a href="http://otherdomain.com/test.html">test</a> <a href="http://mydomain.com/someothertest/otherdir/hi.html">hi</a> '; 

What is the best way to do this? I think something with RegEx, but my RegEx skills: **;)

early!

+5
url php regex hyperlink
source share
3 answers

found a good way:

 $html = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#", '$1http://mydomain.com/$2$3', $html); 

you can use (?!http|mailto) if you also have mailto links in your $ html

+9
source share
 $domain = 'http://mydomain'; preg_match_all('/href\="(.*?)"/im', $html, $matches); foreach($matches[1] as $n=>$link) { if(substr($link, 0, 4) != 'http') $html = str_replace($matches[1][$n], $domain . $matches[1][$n], $html); } 
+4
source share

The previous answer will cause problems with your first and fourth examples, because it cannot include a slash to separate the page from the page name. Admittedly, this can be fixed simply by adding it to the $ domain, but if you do, then href = "/something.php" will be in two.

To give an alternative Regex solution, you can go with something like this ...

 $pattern = '#'#(?<=href=")(.+?)(?=")#''; $output = preg_replace_callback($pattern, 'make_absolute', $input); function make_absolute($link) { $domain = 'http://domain.com'; if(strpos($link[1], 'http')!==0) { if(strpos($link[1], '/')!==0) { return $domain.'/'.$link[1]; } else { return $domain.$link[1]; } } return $link[1]; } 

However, it is worth noting that with a link such as href = "example.html", the link refers to the current directory, none of the methods shown so far will work correctly for relative links that are not in the root directory. To provide a solution that, although more information is needed on where the information came from.

+1
source share

All Articles