0
I’m using the $_SERVER["PATH_INFO"] to pass values to the backend through the url, not in the format ?chave=valor, instead, /:valor/ (identify the colon and return a variable with that name), however, when I have a request for the url //valor/ the value of $_SERVER["PATH_INFO"] is /valor/, causing some problems...
Why? And how to solve?
Do not use
preg_replacethus{$current_route}, usepreg_quote, or better, in this case you wouldn’t even need to.– Guilherme Nascimento
@Guilhermenascimento I supplemented the answer, perhaps it was clearer, but if you think you’re wrong anyway, could you explain it to me? What’s wrong with
{$current_route}?– Costamilam
The problem is to mix string with regular expression, but in general the last preg_replace could be replaced by substr counting the characters of $current_route, pq ai wouldn’t even need to preg_quote to prevent the URL from breaking the regex. The way this code of yours will just break if someone types in a URL like
/[a-z]/manually.– Guilherme Nascimento
@Guilhermenascimento tested with
[a-z]and even with.*in the middle of the URL and kept working, I don’t think you can use thesubstrthis time because of the extension.phpoptional– Costamilam
Already left warned, mixing regex with string can be a problem, to avoid simply use
preg_quoteto escape metacharacters, the substr suggestion was to simplify, but preg_replace and preg_quote even if it gets more code already solves the whole problem.– Guilherme Nascimento
@Guilhermenascimento how would you look? I tried here, unsuccessfully
– Costamilam