PHP useful links

PHP useful links
5 (100%) 1 vote[s]

Here I will update the most interesting and useful links which I found related to php. I encourage you to fill out this list posting your favorite php links in your comments.

HTTP Status Code Checker

FastRoute – Fast request router for PHP

FastRoute library provides a fast implementation of a regular expression based router.

Combined regular expressions

The basic idea for optimizing this kind of problem is to avoid matching the regular expressions one by one and instead compile them into one, big regular expression, so you need to perform only one match.
The transformation is very simple: Basically you just need to OR all the individual expressions together.

Here’s a sample implementation of the whole process:

public function dispatch($uri) {
    if (!preg_match($this->regex, $uri, $matches)) {
        return [self::NOT_FOUND];
    }

    // find first non-empty match (skipping full match)
    for ($i = 1; '' === $matches[$i]; ++$i);

    list($handler, $varNames) = $this->routeData[$i];

    $vars = [];
    foreach ($varNames as $varName) {
        $vars[$varName] = $matches[$i++];
    }
    return [self::FOUND, $handler, $vars];
}

Leave a Reply

Your email address will not be published. Required fields are marked *