1

Hey my routing script isnt working with special chars and i dont have any idea anymore why or how to fix it.

My Url: /test/x/hállò/123
My Router added URL: /test/:varx/:variableZ/123

$route['url'] ="/test/x/:name/123";
$reqMet = $_SERVER['REQUEST_METHOD'];
$route['method'] = "GET";
$reqUrl = "/test/x/hállò/123";
$matches = Array();

$pattern = "@^" . preg_replace('/\\\:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', preg_quote($route['url'])) . "$@D";

if($reqMet == $route['method'] && preg_match($pattern, $reqUrl, $matches)) {
  echo "THIS SHOULD BE THE OUTPUT";
}

So its not working with words like lòl or Háy but i get back the correct Url but with things like article/test/123 all is perfect.

Hope someone have a fix for me. Thanks

EDIT

So this is working

$router->addRoute('GET','/test/x/hállò/123','home/main',false);

URL >> http://localhost/test/x/hállò/123

BUT this not

$router->addRoute('GET','/test/x/:name/123','home/main',false`);

URL >> http://localhost/test/x/hállò/123

:name is for variables which are replaceable like Article id oder whatever.

Juan
  • 65
  • 1
  • 10
  • Possible duplicate of [How can I preg\_replace special character like 'Prêt-à-porter'?](http://stackoverflow.com/questions/2050723/how-can-i-preg-replace-special-character-like-pr%c3%aat-%c3%a0-porter) and also [preg_match and (non-English) Latin characters](http://stackoverflow.com/questions/5424494/preg-match-and-non-english-latin-characters) – node_modules Apr 05 '17 at 06:19
  • @chris85 yes but you should get :S i want to get it – Juan Apr 05 '17 at 06:21
  • @C0dekid nah its not solving my problem – Juan Apr 05 '17 at 06:29
  • `Nope.. not working` means the pattern matched, what is expected here? Simplified reproducible version of your code. https://3v4l.org/8Jccp You regexs also could just be `[a-zA-Z0-9_-]+` the underscore isn't special and the hyphen just needs to be last or first. – chris85 Apr 05 '17 at 06:33
  • In PHP preg_ functions, you need to use `/u` modifier if you want to deal with Unicode chars. Also, are you sure `$reqMet == $route['method']`? If you remove this condition, you get the "THIS IS THE OUTPUT" - [**see this demo**](https://ideone.com/nGatS0). – Wiktor Stribiżew Apr 05 '17 at 06:35
  • You seem to forget to tag the question appropriately. If it is zend-framework, add it. – Wiktor Stribiżew Apr 05 '17 at 07:14
  • @Juan Your question and code need to demonstrate the exact issue. Are you using `preg_` functions? – chris85 Apr 05 '17 at 07:17
  • @WiktorStribiżew It isnt :D its my own – Juan Apr 05 '17 at 07:19
  • @chris85 the code is all i use in this script theres notihinge much more before it – Juan Apr 05 '17 at 07:19
  • Cool, then what pattern is the `:name` translated into? – Wiktor Stribiżew Apr 05 '17 at 07:21
  • This is what it is when its patternd /test/x/([a-zA-Z0-9\-\_]+) – Juan Apr 05 '17 at 08:04
  • so this ([a-zA-Z0-9\-\_]+) should be replace the :name with hálló but it doesnt – Juan Apr 05 '17 at 08:05

1 Answers1

1

While it seems you may need to include several other accented and/or non-English letters, I will only include the letters you mentioned.

An alternative and super-lenient pattern (perhaps not secure enough) would be \\\:[^\/]+

Code:

$route['url'] ="/test/x/:name/123";
$reqUrl = "/test/x/hállò/123";

echo "Input:\n";
var_export(preg_quote($route['url']));

$pattern="@^".preg_replace('/\\\:[a-zA-Z0-9áò_-]+/','([a-zA-Z0-9áò_-]+)',preg_quote($route['url']))."$@D";
//                                         ^^ new letters here: ^^

if(preg_match($pattern, $reqUrl, $matches)) {
    echo "\n\nOutput\n";
    var_export($matches);
}else{
    echo "\n\nNo Match";
}

Displays:

Input:
'/test/x/\\:name/123'

Output
array (
  0 => '/test/x/hállò/123',
  1 => 'hállò',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136