Determine if the path is remote or local

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 these files is local or remote. those. only [5] is remote. Is there any way to do this?

+4
source share
3 answers

You can do this 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; } } 
+6
source

The easiest way is to search for a double slash. Although it would be fair to have a relative path, this will happen after a period (any domain or IP), a GET variable ( .js?variables// ) or another path ( js/path//to.js ). To do this, use the following code.

 foreach ($array as $path) { if (strpos($path, '//') >= max(strpos($path, '.'), strpos($path, '/'))) { #absolute! } else { #relative! } } 
+2
source

Gave this thought. Here is my solution:

 function isLocal($path) { return preg_match('~^(\w+:)?//~', $path) === 0; } 

Although this will not be done for file:///path/to/my/file (should return true ), but I'm not sure if this bothers me right now.

0
source

Source: https://habr.com/ru/post/1499959/


All Articles