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

Tomas Vortuba:
How to Convert Latte Templates to Twig in 27 Regular Expressions
Jul 09, 2018 @ 15:35:33

Tomas Votruba has a post to his site sharing a method for translating Latte templates to Twig templates with the help of the Simplify tool.

Statie - a tool for generating static open-sourced website like this blog or Pehapkari.cz - runs on YAML and Symfony DI Container. That way it's easy to understand by the PHP community worldwide.

But there are some pitfalls left. Like templates - being Latte the only one is a pity. Twig is often requested feature and one of the last big reasons not to use Statie.

Well, it was. Statie will support both Twig and Latte since next version. Are you a Twig fan? As a side effect, I made 27 regular expression to handle 80 % of the Latte to Twig migration for you.

He starts with a bit of explanation of how to the project started and his goals ("investing 5 hours to automate 30-minutes manual work under 10 seconds, so no-one else will have to do that ever again"). He covers the installation of the Symplify tool and how to execute the latte-to-twig-converter on a directory of Latte templates. He also provides some snippets of code you can use if you want to reverse the process and go from Twig to Latte.

tagged: convert latte template twig regular expression symplify tool tutorial

Link: https://www.tomasvotruba.cz/blog/2018/07/05/how-to-convert-latte-templates-to-twig-in-27-regular-expressions/

Nikita Popov's Blog:
The true power of regular expressions
Jun 15, 2012 @ 13:42:57

Nikita Popov has a new (language agnostic) post to his blog today about one of the most powerful things you can use in your development - something that a lot of developers don't understand the true power of - regular expressions.

As someone who frequents the PHP tag on StackOverflow I pretty often see questions about how to parse some particular aspect of HTML using regular expressions. A common reply to such a question is: "You cannot parse HTML with regular expressions, because HTML isn’t regular. Use an XML parser instead." This statement - in the context of the question - is somewhere between very misleading and outright wrong. What I’ll try to demonstrate in this article is how powerful modern regular expressions really are.

He starts with the basics, defining the "regular" part of "regular expression" (hint: it has to do with predictability) and the grammar of the expressions. He talks about the Chomsky hierarchy and how it relates to the "regular" as well as a more complex mapping of expression to language rules. He talks about matching context-free and context-sensitive languages and unrestricted grammars as well.

tagged: power regular expression define theory context

Link:

PHPMaster.com:
Practicing Regular Expressions with Search and Replace
Nov 23, 2011 @ 20:27:59

On PHPMaster.com today there's a new tutorial that shares a few regular expression tips about doing some search and replace in your content.

So how can you practice using regex if you are limited to just using them in your code? The answer is to use a utility, of which there are many, that uses regex for performing search and replace. I’m sure everyone is familiar with the standard “find x and replace it with y” type of search and replace. Most IDEs and text editors have built in regex engines to handle search and replace. In this article I’d like to walk through a series of exercises to help you practice using regex.

His examples are based on Netbeans but can be used in just about any IDE that supports regex (or even just your code). He shows how to match word boundaries, do some grouping, work with back references and doing some search/replace based on multiple groupings.

tagged: regular expression practice search replace boundaries group backreference

Link:

PHPMaster.com:
Regular Expressions
Sep 27, 2011 @ 14:42:28

Regular expressions have always been something that have mystified developers, even those seasoned ones looking to match the most complicated data. If you're just venturing into the world of regex, PHPMaster.com has a good guide to help you wade through some of the basics.

It makes all the sense of ancient Egyptian hieroglyphics to you, although those little pictures at least look like they have meaning. But this… this looks like gibberish. What does it mean? [...] When you’re looking to go beyond straight text matches, like finding "stud" in "Mustard" (which would fail btw), and you need a way to "explain" what you’re looking for because each instance may be different, you’ve come to need Regular Expressions, affectionately called regex.

The include a (somewhat) complicated example regex string and break it down chunk by chunk - groupings, character sets, multiple matching, delimiters and more (the pattern matches valid email addresses). They show how to use it in PHP with preg_match, preg_replace and preg_match_all for different situations.

tagged: regular expression introduction email match tutorial

Link:

Gareth Heyes' Blog:
PHP CSSReg
Aug 23, 2011 @ 15:21:40

Gareth Heyes has a recent post pointing out the port of a project of his, CSSReg (a filtering tool for user-provided stylesheets) over to PHP.

Just a quick post to mention the excellent work by Norman Hippert aka @thewildcat, he successfully converted my Javascript based CSSReg into PHP. I was meaning to do this but never found the time so it’s pretty awesome that not only did thewildcat convert the code but found some nice bugs in my code and fixed them. Great work Norman thanks very much!

You can see a demo of it here and grab the source as a download here. You can find out more about the origins of CSSReg (and some of its siblings) in this other post from Gareth.

tagged: cssreg regular expression javascript port demo

Link:

Leonid Mamchenkov's Blog:
PHP regular expression to match English/Latin characters only
Aug 18, 2011 @ 17:11:44

Leonid Mamchenkov has a quick new post to his blog sharing a regular expression that can be used to check that a string contains only English or Latin characters (no Unicode allowed).

Today at work I came across a task which turned out to be much easier and simpler than I originally thought it would. We have have a site with some user registration forms. The site is translated into a number of languages, but due to the regulatory procedures, we have to force users to input their registration details in English only. Using Latin characters, numbers, and punctuation.

Thankfully the PCRE regular expression engine bundled with PHP makes it simple - it uses a standard regular expression without anything special to accommodate for Unicode characters. He notes that adding the "/u" modifier to the expression makes it "totally malfunction" (where strings are treated as UTF-8). If you'd like an example of some of the tricks that go into supporting Unicode in a regex, see this comment in the PHP manual.

tagged: regular expression example english latin unicode

Link:

Zoomzum Blog:
10 Powerful PHP Regular Expression For Developers
Jul 27, 2011 @ 14: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

Link:

NETTUTS.com:
Caching, YQL, and Regular Expressions
Nov 10, 2010 @ 16:07:14

On NETTUTS.com today there's a new tutorial showing you how to work with YQL for pulling information from remote feeds and PHP (with regular expressions) to handle filtering and caching.

In today's tutorial, we're going to mix a handful of technologies. First, we'll review how to implement a simple form of text file caching with PHP. To illustrate this technique, we'll use the wonderful YQL to query Twitter’s search API for a list of tweets which contain the string, "nettuts." Finally, we’ll experiment with PHP's regular expression capabilities, and will turn all Twitter usernames and urls into clickable links.

They show you (with the help of a few screenshots) how to get the YQL system to work with your PHP scripts and how you can fun a simple query against it. They use a simple file-based caching technique and a regular expression (not the simplest thing, mind you) to extract usernames and links from the results.

tagged: yql caching regular expression tutorial twitter

Link:

Noupe.com:
Getting Started with PHP Regular Expressions
Aug 24, 2009 @ 12:59:10

Noupe.com has posted a new guide to getting started with regular expressions in PHP. Regular expressions can be one of the most powerful tools at your disposal when used correctly.

The main purpose of regular expressions, also called regex or regexp, is to efficiently search for patterns in a given text. These search patterns are written using a special format which a regular expression parser understands.

They cover some of the basics - special characters and some examples using them (like $, d, and *) - before getting into the preg_* functions in PHP. They also include some handy examples to validate things like US zip codes, email format validation and removing duplicated words in a section of text.

tagged: regular expression tutorial preg

Link:

CatsWhoCode.com:
15 PHP regular expressions for web developers
Jul 28, 2009 @ 15:09:49

On CatsWhoCode.com today there's a new article listing fifteen handy regular expressions you might find useful in your day-to-day development (as well as a brief introduction to what regular expressions are).

Regular expressions are a very useful tool for developers. They allow to find, identify or replace text, words or any kind of characters. In this article, I have compiled 15+ extremely useful regular expressions that any web developer should have in his toolkit.

Here's just some of the expressions on the list:

  • Enlight a word from a text
  • Remove repeated words (case insensitive)
  • Matching a XML/HTML tag
  • Matching hexadecimal color values
  • Parsing Apache logs
tagged: regular expression tip example

Link:


Trending Topics: