Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse_url() does not parse a specific url

Tags:

php

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.

like image 343
Felix Avatar asked May 30 '26 16:05

Felix


1 Answers

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

like image 111
Sebastian Brosch Avatar answered Jun 01 '26 07:06

Sebastian Brosch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!