its working for me, try it too
<?php
echo path_to_absolute(
"../images/example.jpg", /* image url */
"http://php.net/manual/en/" /* current page url */,
false /* is your url containing file name at the end like "http://server.com/file.html" */
);
function path_to_absolute( $src, $base = null, $has_filename = false ) {
if ( $has_filename && !in_array( substr( $src, 0, 1 ), array( "?", "#" ) ) ) {
$base = dirname( $base )."/";
}
else {
$base = rtrim( $base, "/" )."/";
}
if ( parse_url( $src, PHP_URL_HOST ) ) {
/* Its full url, so return it without modifying */
return $src;
}
if ( substr( $src, 0, 1 ) == "/" ) {
/* $src begin with a slash, find server host and, join it with $src */
return str_replace( parse_url( $base, PHP_URL_PATH ), "", $base ).$src;
}
/* remove './' from $src, we dont need it */
$src = ( substr( $src, 0, 2 ) === "./" ) ? substr( $src, 2, strlen( $src ) ) : $src;
/* check how many times we need to go back **/
$path = substr_count( $src, "../" );
$src = str_ireplace( "../", "", $src );
for( $i = 1; $i <= $path; $i++ ) {
if ( parse_url( dirname( $base ), PHP_URL_HOST ) ) {
$base = dirname( $base ) . "/";
}
}
return $base . $src;
}
?>
example usage..
here we finding links from php.net
as there are so many relative links
<?php
$url = "http://www.php.net/manual/en/tokens.php";
$html = file_get_contents( $url );
$dom = new DOMDocument;
@$dom->loadHTML( $html );
$dom->preserveWhiteSpace = false;
$links = $dom->getElementsByTagName( 'a' );
foreach( $links as $link ) {
$original_url = $link->getAttribute( 'href' );
$absolute_url = path_to_absolute( $original_url, $url, true );
echo $original_url." - ".$absolute_url."\n";
}
/** prints...
* / - http://www.php.net/
* ...
* control-structures.while.php - http://www.php.net/manual/en/control-structures.while.php
* control-structures.do.while.php - http://www.php.net/manual/en/control-structures.do.while.php
* ...
* /sitemap.php - http://www.php.net/sitemap.php
* /contact.php - http://www.php.net/contact.php
* ...
* http://developer.yahoo.com/ - http://developer.yahoo.com/
* ...
* ?setbeta=1&beta=1 - http://www.php.net/manual/en/tokens.php?setbeta=1&beta=1
* ...
* #85872 - http://www.php.net/manual/en/tokens.php#85872
**/
?>