I would like to add root path of the site for those anchor tag which have not root path using php dom document, Till now a have made a function to do this with str_replace f
Instead of if(strpos($href,'www' > 0))
you should use if(strpos($href,'www') !== false)
.
The > 0
was inside the function-call (strpos()
).
I see some problems in your code:
How to detect if a URL is a relative one.
Relative URLs don't specifiy a protocol. So I would check for that to determine whether or not a href attribute is a fully qualified (absolute) URI or not (Demo):
$isRelative = (bool) !parse_url($url, PHP_URL_SCHEME);
Resolving a relative URL to a base URL
However this won't help you to properly resolve a relative URL to the base URL. What you do is conceptually broken. It's specified in an RFC how to resolve a relative URI to the base URL (RFC 1808 and RFC 3986). You can use an existing library to just let the work do for you, a working one is Net_URL2:
require_once('Net/URL2.php'); # or configure your autoloader
$baseUrl = 'http://www.example.com/test/images.html';
$hrefRelativeOrAbsolute = '...';
$baseUrl = new Net_URL2($baseUrl);
$urlAbsolute = (string) $baseUrl->resolve($hrefRelativeOrAbsolute);