so I made an routing system in PHP…
So I made a route called /post/:id, but whenever I print_r my $matches I get this:
Array ( [0] => /post/10 [d] => 10 [1] => 10 ) 1
That ‘d’ in the array should obviously be ‘id’, anyone know how to fix this?
Thx
<?php
public function setPattern($pattern)
{
$this->_pattern = $pattern;
$this->_regex = preg_replace('#:([a-z])+$#', "(?P<$1>[^/]+)", $pattern);
}
public function match($uri)
{
if (!preg_match("#" . $this->_regex . "$#", $uri, $matches))
{
return false;
}
else
{
return $matches;
}
}
You only capture one character in your
preg_replacecall. Try this and put the + sign in the capture to get multiple characters: