Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if path is remote or local

Tags:

php

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?

like image 896
mpen Avatar asked Aug 31 '13 20:08

mpen


2 Answers

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;
    }
}
like image 156
dev-null-dweller Avatar answered Oct 27 '22 00:10

dev-null-dweller


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!
    }
}
like image 27
Connor Peet Avatar answered Oct 26 '22 23:10

Connor Peet