Welcome Guest, Not a member yet? Register   Sign In
Numbers in URI
#1

[eluser]JonoB[/eluser]
I have a problem with numbers being included in certain portions of a uri segment. For example, when a number is included after a : (colon), then I get an error.

Code:
class Foo extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
    }

    function bar()
    {
        var_dump($this->uri->segments);
    }
}

?>


http://localhost/ci/foo/bar/
Code:
array
  1 => string 'foo' (length=3)
  2 => string 'bar' (length=3)


http://localhost/ci/foo/bar/test
Code:
array
  1 => string 'foo' (length=3)
  2 => string 'bar' (length=3)
  3 => string 'test' (length=4)


http://localhost/ci/foo/bar/2
Code:
array
  1 => string 'foo' (length=3)
  2 => string 'bar' (length=3)
  3 => string '2' (length=1)


http://localhost/ci/foo/bar/test:something
Code:
array
  1 => string 'foo' (length=3)
  2 => string 'bar' (length=3)
  3 => string 'test:something' (length=14)


http://localhost/ci/foo/bar/test:2
FAIL
Code:
A PHP Error was encountered
Severity: Warning
Message: parse_url(/foo/bar/test:2) [function.parse-url]: Unable to parse URL
Filename: core/URI.php
Line Number: 176


http://localhost/ci/foo/bar/2:test
Code:
array
  1 => string 'foo' (length=3)
  2 => string 'bar' (length=3)
  3 => string '2:test' (length=6)


Am I missing something obvious here?
#2

[eluser]Sverri[/eluser]
[quote author="JonoB" date="1300126903"]http://localhost/ci/foo/bar/test:2
FAIL
Code:
A PHP Error was encountered
Severity: Warning
Message: parse_url(/foo/bar/test:2) [function.parse-url]: Unable to parse URL
Filename: core/URI.php
Line Number: 176
[/quote]

It seems parse_url() has a problem with the colon in your URI.

Try changing line 176 in core/URI.php...

Code:
$uri = parse_url($uri, PHP_URL_PATH);

To this...

Code:
if (strpos($uri, ':') !== FALSE)
{
  $uri = parse_url(str_replace(':','~!!!~',$uri), PHP_URL_PATH);
  
  $uri = str_replace('~!!!~',':',$uri);
}
else $uri = parse_url($uri, PHP_URL_PATH);

You can also, if at all possible, just use / instead of : and then just fetch the number as a segment.
#3

[eluser]Sverri[/eluser]
Okay here is a workaround that should fix it in a minimally invasive manner, provided I am not missing something.

Code:
// Parse_url will fail on segments that look like "foo:number". It is the
// colon that appears to be causing it (probably because parse_url thinks
// that it is a port number, and thus malformed). So, if the pattern is
// found it needs to be worked around.
if (preg_match("/:\d/", $uri))
{
  // Substitue colons, parse the URI, reinstate colons again
  $uri = str_replace('{C0L0N}',':',parse_url(str_replace(':','{C0L0N}',$uri),PHP_URL_PATH));
}
// Otherwise parse_url should work without problems
else $uri = parse_url($uri, PHP_URL_PATH);




Theme © iAndrew 2016 - Forum software by © MyBB