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

php[architect]:
Pro Parsing Techniques With PHP, Part Three Using Regular Expressions
Aug 29, 2018 @ 15:55:15

On the php[architect] site they've posted the latest part of their series of magazine excerpts sharing methods for parsing text with PHP. In this third (and last) part the author focuses on the use of regular expressions. This article was originally printed in the magazine's August 2018 edition.

This is the final installment of a set of three articles offering strategies for parsing text with PHP. The first article described the basics of parsing, followed by an article on developing fault tolerant parsing strategies. This article is dedicated to regular expressions.

Regular expressions, or sometimes simply called regex, represent a powerful set of tools which allow developers to split strings, perform character substitutions, and extract text based on matched patterns. The patterns, used in regular expressions, are an actual language that describe combinations of type castings and values that match the text you want to split, substitute, or extract. Regular expressions are an enormously powerful tool for the developer who understands them.

The tutorial starts by walking through some of the basics of what regular expressions are and the functions provided by PHP (using preg_*) to work with them (including basic code examples). It then briefly talks about pattern matching with regular expressions and gives a more specific example showing phone number extraction.

tagged: phparchitect august18 parsing text series part3 regularexpression tutorial

Link: https://www.phparch.com/2018/08/pro-parsing-techniques-with-php-part-three-using-regular-expressions/

Pineco.de:
Examples, Tools and Resources for Regular Expressions
Aug 01, 2018 @ 16:27:05

On the Pineco.de blog there's a tutorial posted introducing regular expressions and providing some examples and links to external resources/tools to help put them to use in your code.

Using Regular Expressions is not easy. Mostly we have the feeling we need to learn a new language on the top of those we already know. But, the power and the flexibility that RegEx provides, make it worthy to learn. Take a look at some useful patterns, tools, and sources!

The examples they provide show the matching all the words and matching all the content between specified tags. They end the post linking to several helpful tools including the Laracasts regular expression video and the regexr.com testing tool.

tagged: regularexpression tool resources tutorial introduction example

Link: https://pineco.de/examples-tools-and-resources-for-regular-expressions/

Nicolas Grekas:
Making Symfony router lightning fast - 2/2
Feb 22, 2018 @ 18:54:30

Nicolas Grekas has posted the second part of his look at the work that was done to increase the performance on the router in version 4 of the Symfony framework. In part one he covered some of the basic changes made to the router for faster matching. In this latest article he covers some of the "tweaks" made on top of this work to help improve things even more.

In Making Symfony’s Router 77.7x faster - 1/2, we learned how to build a faster URL matcher, using hash-map lookups for static routes, and combined regular expressions for routes with placeholders, while preserving all the advanced features of the Symfony router. However, more work was needed for some real world apps, as at least one of them experienced a slow down. Let’s see how fixing this provided us with (one of) the fastest PHP routers out there.

He then starts working through some of the newer changes to help "reclaim" some of the performance loss in certain situations. He talks about same-prefix route ordering, subpatterns and placeholders to change how the combined regular expressions perform the matching on the incoming URL. The result is an even more performant routing system that's 77 times faster than what they started with.

tagged: symfony routing performance regularexpression regex improvement series part2

Link: https://medium.com/@nicolas.grekas/making-symfony-router-lightning-fast-2-2-19281dcd245b

Symfony Blog:
New in Symfony 4.1: Fastest PHP Router
Feb 16, 2018 @ 16:48:33

On the Symfony blog they have a post covering the result of some changes to how the framework handles incoming requests in Symfony 4: a performance and speed increase in the router making it even better than before.

Symfony 4 is the fastest PHP framework according to independent benchmarks, but we are continuously working on making it faster. In Symfony 4.1, we improved the Routing component to make it much faster when matching incoming URLs.

The post starts with a look at the two functional pieces of route handling: the generation of a URL to match and the matching the framework performs. Symfony 4 has sped things up by creating a "matcher" class during the compilation phase using some of these suggestions. The biggest change was to modify the regular expression URL matching to combine all patterns into one, reducing the number of calls to preg_match and locate the correlating route. The new routing requires no changes in your current Symfony 4 application, it just makes all of the updates it needs behind the scenes during compilation.

tagged: symfony symfony4 router speed performance regularexpression

Link: http://symfony.com/blog/new-in-symfony-4-1-fastest-php-router

Ignace Nyamagana Butera:
Regular expressions documented in PHP
Oct 20, 2015 @ 15:35:09

In this post to his site Ignace Nyamagana Butera shares a a helpful thing you can do with the regular expression handling in PHP to help make it a bit more clear: embedded comments.

One of the most challenging aspect of using regular expressions is documenting them. More than often you end up with a complex expression that even you as a creator have a hard time documenting. While reading sitepoint recent article around regular expressions I was intrigued that this article did not feature any tips on how to document them. So let’s take a very complex regular expression and see how we could improve its documentation.

He gives an example of a complex regular expression used to parse a URI into its different parts, including an example URI and the resulting parsed array. He points out that, while the regular expression works fine, it's easy to forget what each part does and as it is quite complex. To help remedy this, he includes an example of a commented regular expression (available since PHP 5.2) where you have two options:

  • add in named subpatterns with a special < and > notation
  • put in literal comments by breaking up the regex into multiple lines and using the hash (#) to mark of the comment at the end of the line

He includes code examples of both of these, resulting in a much clearer, memorable regular expression where the increased number of lines is a good trade-off for clarity.

tagged: regularexpression regex document comment named subpattern tutorial

Link: http://nyamsprod.com/blog/2015/regular-expressions-documented-in-php/

SitePoint PHP Blog:
Demystifying RegEx with Practical Examples
Sep 25, 2015 @ 17:30:19

On the SitePoint PHP blog they've posted a tutorial from author Nicola Pietroluongo that wants to help demystify regular expressions with a few more real-world examples. He doesn't teach the foundations of regular expressions here and instead opts for a more "cookbook" approach with lots of little examples.

A regular expression is a sequence of characters used for parsing and manipulating strings. They are often used to perform searches, replace substrings and validate string data. This article provides tips, tricks, resources and steps for going through intricate regular expressions.

He starts with some basic tips around creating good regular expressions for your application: knowing the scenario you're matching, planning the requirements and implementing the match itself. His example expressions include matching for:

  • simple passwords matching a policy
  • valid URL matching
  • HTML tag patterns
  • finding duplicated words

Each example comes with the regular expression itself and an explanation of how it's doing the matching, breaking it down into each piece of the regex puzzle and how it relates to the match overall.

tagged: regularexpression regex practical example tutorial scenario requirements

Link: http://www.sitepoint.com/demystifying-regex-with-practical-examples/

Dylan Bridgman:
Building a basic router
Aug 14, 2015 @ 14: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

Nikita Popov:
Fast request routing using regular expressions
Feb 19, 2014 @ 15:03:07

In his latest post Nikita Popov talks about routing and regular expresions. He also shares some work he's done to create a fast request router using them in "userland" code instead of a C extension.

Some time ago I stumbled on the Pux routing library, which claims to implement a request router that is many orders of magnitude faster than the existing solutions. In order to accomplish this, the library makes use of a PHP extension written in C. However, after a cursory look at the code I had the strong suspicion that the library was optimizing the wrong parts of the routing process. [...] To investigate the issue further I wrote a small routing library: FastRoute. This library implements the dispatch process that I will describe below.

He includes some benchmarks against the results from a C-based routing engine showing his solution performing slightly better. What he's really talking about, though, is the dispatch process in general, not just his implementation. He talks about "the routing problem" many engines face - having to loop through a potentially large set of routes to find a match. He offers an alternative using regular expressions and compiling all of the routes down into one large expression. He includes a simple implementation of the method and reruns the same benchmarks with some different results. He offers one potential solution for speeding it up using "chunked expressions" to break it down into more manageable matching. He includes benchmarks for this last solution as well, showing a slight improvement.

tagged: regularexpression routing dispatch engine chunk compile

Link: http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html

Sameer Borate:
Constructing hard regular expressions with VerbalExpressions
Aug 12, 2013 @ 16: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

Refulz.com:
Special characters in Regular Expressions - Part 1
Jun 12, 2012 @ 13:39:11

On the Refulz.com site they've posted the first part of a series about the basics of using special characters regular expressions (both in PHP and outside of it).

With this post, we continue to explore the Regular expressions. The first post of the Learning Regular Expression series introduced Regular Expressions. The first post covers the regular expression delimiters and the “i” pattern modifier. In the language of regular expression, there is a special meaning of certain characters.

In this article they show the use of characters like the caret, asterisk, dot and dollar symbol to modify your expressions to handle special cases, matching for more than one character and the start and end of strings.

tagged: regularexpression tutorial introduction special character

Link:


Trending Topics: