Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Dylan Bridgman:
Building a basic router
Aug 14, 2015 @ 09:37:45

Dylan Bridgman has posted a new tutorial talking about building one of the key pieces of any framework (and most applications) to help get requests to the right place - a basic routing system.

There is always value in learning about the internals of the frameworks and libraries we use. It allows for a deeper understanding of the problem being solved and appreciation of the work that has gone into these projects. So today I will be building a basic router to explore this fundamental part of even the smallest framework. The idea is not to create something complete or production-ready but rather the minimum set of features needed to be considered a router.

He creates a simple script that handles both static and variable routes as well as throw an error when a route match isn't found. He starts off talking about the structure of URLs and shows the setup of a rewrite rule to forward all requests to an index page (where the router lives to handle them). Then he talks about the structure of the routing table and how to structure the route-to-action formatting. He opts for a simple PHP array with a closure as the action portion as a starting place. He shows how this is useful for static route matching but upgrades to regular expression matching (passed through a preg_match) to allow variables.

tagged: basic router framework static variable regularexpression regexp

Link: https://medium.com/@dylanbr/building-a-basic-router-b43c17361f8b

Sameer Borate:
Constructing hard regular expressions with VerbalExpressions
Aug 12, 2013 @ 11:19:40

Sameer Borate has an post to his site sharing a library that could help you in creating more complex regular expressions a bit more simply. He introduces the VerbalExpressions library and an example of its use.

Most newbie (and some seasoned) programmers have difficultly constructing Regular Expressions. Many a times one needs to create a Regexp quickly to test a a particular piece of code. However, not being comfortable withe Regexps can be a problem. VerbalExpressions is a PHP library that enables you to construct regular expressions using natural language like constructs. Think of it like a DSL for building Regexps.

His example checks to see if a given string is a valid URL (yes, filter_var can do this too, but stick with him). He shows how to use the library's fluent interface to construct the regexp, export it as a string or just pass in the object as the regular expression in something like preg_match. The VerbalExpressions library is hosted over on Github.

tagged: verbalexpressions regularexpression regexp library

Link: http://www.codediesel.com/php/constructing-hard-regular-expressions-with-verbalexpressions

Zoomzum Blog:
10 Powerful PHP Regular Expression For Developers
Jul 27, 2011 @ 09:02:10

On the Zoomzum blog there's a new post with ten regular expressions PHP developers can use to accomplish some common tasks (like email validation and date formatting checks).

Regular expression for the PHP developers, on of the most popular tool for validating data is the regular expression. In this list we provides some validation – string match, password match validation, email address validation, date format and many more which helps developer to make their application more fast and easy to execute. [...] Have you note that, regular expressions are more slower than the basic string function, its takes a short time to execute than any others.

Included in their list are things like:

  • Password Match Validation
  • Validate URL
  • Validate URL using Preg_match
  • UK Postcode Validation
  • SSN,ISBN and Zipcode Validation

A few of these could be done with either one or two string calls or some of the filtering functions that are included in PHP.

tagged: regular expression hint list validate regexp