I have an array that looks like this:
Array
(
[0] => public\js\jade\runtime.js
[1] => public\js\templates.js
[2] => public\js\underscore.js
[3] => public\js\underscore.string.js
[4] => public\js\main.js
[5] => //ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
)
I need to determine which of those files is local or remote. i.e., only [5] is remote. Is there a method for doing this?
You can do it with parse_url
. Example:
function is_path_remote($path) {
$my_host_names = array(
'my-hostname.com',
'www.my-hostname.com'
);
$host = parse_url($path, PHP_URL_HOST);
if ($host === NULL || in_array($host, $my_host_names)) {
return false;
} else {
return true;
}
}
The simplest way would be a search for a double slash. Although it would be valid to have in a relative path, it would come after a period (any domain or IP), a GET variable (.js?variables//
), or another path (js/path//to.js
). The following code accounts for these.
foreach ($array as $path) {
if (strpos($path, '//') >= max(strpos($path, '.'), strpos($path, '/'))) {
#absolute!
} else {
#relative!
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With