chriso/klein.php — pull request #253 by Rican7 (2014-10-27 03:55:21+00:00)
Title: Bugfix - Regex processing causing overquoting/overescaping

This PR is courtesy of the incredible bug report and fix suggestion in #251, thanks to @taliesinnz.

Essentially, when creating a route match string with multiple values that needed escaping (like a `.` character) before processing, the value was being overly escaped due to looping over the similar values multiple times and calling `str_replace()` which would replace the found value globally in the string.

The fix could have been made by checking the match's position in the string and doing a `substr_replace()`, but @taliesinnz suggested a more efficient and cleaner way of fixing this: using `preg_replace_callback()`.

This fix has the wonderful side-effect of increasing performance of regular expression compilation, as it doesn't have to break apart the string and replace each location multiple times.

Benchmarks show that this method is more linear (and consistent) in processing time, so this is great. :+1:

While completing the fix, I made sure to also make the changes in the `getPathFor()`, to gain the same benefits. I also did some validation and sanitization of the route compilation regular expression.

Thanks again for incredible help and contribution @taliesinnz!

![aceventurathumbsup](https://cloud.githubusercontent.com/assets/742384/4786108/0e2ab988-5d8d-11e4-813c-dc39c081ae8b.gif)