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

Nicolas Grekas:
Making Symfony router lightning fast - 2/2
Feb 22, 2018 @ 12: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

Ignace Nyamagana Butera:
Regular expressions documented in PHP
Oct 20, 2015 @ 10: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 @ 12: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/

Five Hidden Gems of Laravel
Aug 22, 2014 @ 11:51:20

The NetTuts.com site has posted a list of their five hidden gems in Laravel, a popular PHP framework. They look at a wide range of these "hidden" features that can help make your Laravel experience even better.

Many developers who use Laravel are probably only barely scratching the surface of what the framework has to offer. While the documentation does cover the most common use cases and the obvious features, it doesn’t cover everything. Don’t get me wrong, the documentation is fine, it’s just that there’s so much you can do, it’s hard to document everything. Because of that, we're going to take a look at some of the hidden gems that lurk within Laravel.

The five items on their list come complete with summaries about the feature, when they were added, if they're documented and a code sample with them in use:

  • Cascading Views
  • Collections (with sorting, filtering and pagination)
  • Regular Expression Filters
  • The Message Bag
  • Fluent
tagged: hidden gems laravel framework views collections regex filter message fluent

Link: http://code.tutsplus.com/articles/five-hidden-gems-of-laravel--cms-21907

Black Magic with Regular Expresions
Nov 29, 2012 @ 10:21:47

If you've been mystified by regular expressions in the past and want to learn more about their effective use, you should check out these two webcasts from Qafoo - "Black Magic with Regular Expressions" and "Understanding Regular Expressions".

We are happy to announce the availability of my second Regular Expression webinar recording. It was a fun event, which I used to explain all the attendees a little bit more about the PCRE Regular Expression engine available in PHP and some other languages. I am covering some slightly advanced topics, like subpattern options, unicode and backtracking aka. greediness and performance.

You can watch these two recordings over on the Zend.com site:

tagged: blackmagic regularexpressions regex webcast recording zend


Advanced Regular Expression Tips and Techniques
Apr 19, 2010 @ 09:47:04

NETTUTS.com has posted an advanced guide to regular expressions that just happens to use the PHP PCRE functions to do the matching.

Regular Expressions are the Swiss Army knife for searching through information for certain patterns. They have a wide arsenal of tools, some of which often go undiscovered or underutilized. Today I will show you some advanced tips for working with regular expressions.

They have it broken out into several tips including:

  • Using Callbacks
  • Lookahead and Lookbehind Assertions
  • Filtering Patterns
  • Named Subpatterns

There's also a section called "Don’t Reinvent the Wheel" with a few handy expressions to do common things like parse [X]HTML and validating form input.

tagged: advanced regularexpression regex tutorial tip


PCRE Regex Spotlight: K
Aug 25, 2009 @ 10:28:44

On the PHPFreaks.com blog today there's a quick new post looking at one of the special backslash strings that doesn't get talked about very much - K - but is quite powerful.

One backslash sequence that doesn’t get much attention is K. What this handy little assertion does is match the position of whatever string comes before it in the pattern, then it in essence resets the match. At that point it starts a new match with whatever comes after K from the current location in the subject string.

They include a series of PHP code examples showing how use can use it to work around some of the issues with lookbehind assertions. They also include a few benefits and drawbacks of using them over lookbehinds.

tagged: regularexpression regex assertion backslashk


Robert Basic's Blog:
Playing with Zend_Navigation and routes
Aug 10, 2009 @ 08:13:12

In this new post to his blog Robert Basic looks at the Zend_Navigation component of the Zend Framework and how it can be used to more correctly handle bad requests.

I wanted to set up routes in such way that when a user requests a page, all requests for non-existing controllers/modules are directed to a specific controller (not the error controller). In other words, if we have controllers IndexController, FooController and PageController, anything but http://example.com/index and http://example.com/foo is directed to the PageController.

Using the Zend_Controller_Router_Route_Regex component he creates an expression that matches anything but the two allowed controllers and pushes them back over to the "page" controller. The new route is put in place via an addRoute call. He also shows how to use the Zend_Navigation component to manage the navigation structure for this application, defining a PHP array of the nested sitemap if the site.

tagged: zendnavigation route regex tree


Make Me Pulse:
Using the Zend Framework URL rewriting
Jan 15, 2009 @ 07:55:15

New from the Make Me Pulse blog is this quick tutorial about bending the URL rewriting that the Zend Framework does to match whatever your needs might be.

Today for a good website’s referencement in Google, it’s necessary to have an URL rewriting. [...] If your application is based on ZF, we have a htaccess base file which will redirect all php files to the boostrap (what is the bootstrap ?), and ZF classes will manage all redirection rules. How to implement the URL rewriting with ZF classes ?

He sets up a config file (an ini file) with the routing instructions the framework will need to adhere to and shows how to get the application to include it and match against it for routing rules. His example sets a default route and several regular expression-based routes to remap requests right where they need to go.

tagged: zendframework config file ini routing regex default route tutorial


Kevin Waterson's Blog:
Introduction to PHP Regex
Sep 09, 2008 @ 12:59:06

Kevin Waterson has posted an extensive tutorial to his site today giving a great look at regular expressions in PHP (and just regular expressions in general).

At its most basic level, a regex can be considered a method of pattern matching or matching patterns within a string. In PHP the most oft used is PCRE or "Perl Compatible Regular Expressions". Here we will try to decypher the meaningless hieroglyphics and set you on your way to a powerful tool for use in your applications. Do not try to understand all this in a single sitting. Instead, take in a little and come back as you grasp various concepts.

He starts with simple string matching, showing how to get values from the middle, end and beginning of it then moves on to the next more powerful step - using meta characters. He outlines these as well as sequences and modifiers. There's plenty of examples here so there's almost no chance of getting lost.

It's a great tutorial that I'd recommend to anyone who things regular expressions are "just too hard" or it looking to figure out their power.

tagged: introduction tutorial regularexpression regex beginner