3

I've got a Problem with PHP parse_url().

My url looks like:

$url = "_search?q=sku:89399";
var_dump(parse_url($url));

According to this, var_dump() returns bool(false) saying this is not a correct url.

But using an url like this works?!

$url = "_search?q=sku:202490";
var_dump(parse_url($url));

var_dump() returns array(2) { ["path"]=> string(7) "_search" ["query"]=> string(12) "q=sku:202490" }

So do you have any idea, why sku:202490 works and and sku:89399 does not ?

Thank you so much!

UPDATE: Thank you all for your comments. According to the first few comments, i can confirm that the number is being interpreted as port number if it has 5 or less digits. So if a number with 5 or less digits is bigger than 65535 parse_url() will not work.

13
  • 1
    RTM php.net/manual/en/function.parse-url.php Commented Dec 1, 2015 at 12:44
  • I'd hazard a guess that 89399 falls within the valid range of values that could be identified as a port (if it checks for number of digits), but that "_search?q=sku is not valid as a protocol, so it complains Commented Dec 1, 2015 at 12:47
  • @Fred-ii- yeah but according to the manual i see no reason why 202490 should work and 89399 not ... thank you Commented Dec 1, 2015 at 12:48
  • 1
    Alex - this is pushing the minimum limits of what might be considered partial for a url though Commented Dec 1, 2015 at 12:50
  • 1
    this is a bug of sorts, there are a couple of bug reports regarding this, fixed in PHP7. bugs.php.net/bug.php?id=55511 (PHP 5.4), bugs.php.net/bug.php?id=66813 (PHP 5.5.9). Fix is here github.com/php/php-src/commit/… Commented Dec 1, 2015 at 13:03

1 Answer 1

1

You have the possibility to set a "dummy" scheme to the specific url:

Example #1

$url = "_search?q=sku:89399";
var_dump(parse_url("test://".$url));

Output #1

array(3) {
    ["scheme"]=> string(4) "test"
    ["host"]=> string(7) "_search"
    ["query"]=> string(11) "q=sku:89399"
}

Your code is working for PHP >= 7.0.0 beta1!

Example #2

$url = "_search?q=sku:89399";
var_dump(parse_url($url));

Output #2 (PHP version >= 7.0.0 beta1)

array(2) {
    ["path"]=> string(7) "_search"
    ["query"]=> string(11) "q=sku:89399"
}

Output #2 (PHP version < 7.0.0 beta1)

bool(false)

With the above information, it is possible to write a own function to do this. Try the following one:

<?php
function custom_parse_url($url) {
    if (version_compare(phpversion(), '7.0.0beta1') < 0) {
        return parse_url('test://'.$url);
    } else {
        return parse_url($url);
    }
}

$url = "_search?q=sku:89399";
var_dump(custom_parse_url($url));

Here you can find a working example: https://3v4l.org/dN2bP

Sign up to request clarification or add additional context in comments.

2 Comments

If all you want is the query, then this is a workaround. Otherwise, the parse result is simply incorrect with the dummy scheme.
Yes of course, but you could extend the function to give one more parameter to define the dummy scheme. Into the function you can remove the scheme if value equals the second parameter.

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.