0

I am compressing some CSS and JS files and have few methods that use parse_url to get the url components. Just found out that prior to php 5.3.28 there is no host and my compression function heavily rely on it in order to get the correct paths.

So on php 5.3.28 < I get

Array
(
    [path] => //fonts.googleapis.com/css
    [query] => family=Open+Sans
)

and 5.3.28 >

Array
(
    [host] => fonts.googleapis.com
    [path] => /css
    [query] => family=Open+Sans
)

can anyone post a possible replacement function or the actual 5.3.28 > parse_url or a regex I could work with.

Any help is appreciated.

Benn
  • 4,840
  • 8
  • 65
  • 106
  • The `host` is split out for prior PHP versions too. You're just not passing in an [actual **URL**](https://tools.ietf.org/html/rfc3986#page-6) as required. Later versions only got lenient with absent scheme prefixes. – mario Nov 29 '14 at 18:26
  • Look at the [source code](https://github.com/php/php-src/blob/88ca46d92bc1c426e7c7f7313f0fd2b7dcc33cf6/ext/standard/url.c). I think you should start looking from line 90. My advice: just don't work with any php version lower than 5.3. Preferably work with 5.4 or 5.5. Aside from performance and bug fixes there are also security fixes and more awesome features. – HamZa Nov 29 '14 at 18:27

1 Answers1

1

As stated in one of the comments, prior the 5.4.7 version, PHP will not recognize the host part for urls without the scheme. Check parse_url() returns an error when example.com is passed question for a few possible solutions, one of them is to manually add a default scheme, or, you can try this example.

$url = '//fonts.googleapis.com/css?family=Open+Sans';
$url_parts = parse_url( $url );

if ( !isset( $url_parts['host'] ) ) {
    $part = ( isset( $url_parts['path'] ) ? $url_parts['path'] : '' ) . ( isset( $url_parts['query'] ) ? '?' . $url_parts['query'] : '' ) . ( isset( $url_parts['fragment'] ) ? '#' . $url_parts['fragment'] : '' );    
    $host = strstr( $url, $part, true );
    $url_parts['host'] = substr( $host, strpos( $host, '//' ) + 2 );
}
Community
  • 1
  • 1
Danijel
  • 12,408
  • 5
  • 38
  • 54