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.

2
  • The host is split out for prior PHP versions too. You're just not passing in an actual URL as required. Later versions only got lenient with absent scheme prefixes. Commented Nov 29, 2014 at 18:26
  • Look at the source code. 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. Commented Nov 29, 2014 at 18:27

1 Answer 1

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 );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.