Jump to content

Archived

This topic is now archived and is closed to further replies.

Philip

README: PHP Regex Resources & FAQs

Recommended Posts

This topic is to provide information for those looking for help with regex in PHP. Below are some great resources and FAQs:

 

Table of Contents - You can find the answers to these questions below in this thread:

  1. I need help with some regex, how should I post?
  2. When shouldn't I use regex?
  3. PHP kept crashing on preg_match* on large strings, why?

Some Resources

Share this post


Link to post
Share on other sites

I need help with some regex, how should I post?

Regex can be a tricky subject, especially when you need some help with it. Here is the bare minimum on what you should include with your post:

  1. Describe your problem
  2. Give lots of sample input data
  3. Give the matching expected data for the input data you gave (this is important!)
  4. If you already have some code (which you should!), show us the code and tell us what you think is going wrong with it
  5. We assume its a PHP regex related as this is PHP Freaks, but you still need to specify (if not obvious) if you are talking about POSIX (ereg) or PCRE (preg) flavor.

Share this post


Link to post
Share on other sites

When shouldn't I use regex?
 
It may be cool and hip to be a "regex guru" (it is!). And regex may be powerful and sexy (it is!).  But more often than not, you don't really need regex to solve your coding dilemma.  In reality, regex is almost always either too powerful or not powerful enough for the job. It has an extremely small set of circumstances where it's the best tool for the job. So, before you try to use regex, ask yourself if you really need it.  
 
A good rule of thumb is to assume you don't need regex, and turn to it when you are certain that no other option exists.  
 
 
Too powerful
 
There are many string and substring functions that will handle things much more efficiently than regex. For example:

  • Want to know if the value has only numbers or letters or alphanumeric etc? Use one of the ctype functions
  • Want to know if "foobar" is in "this is the foobar moment in time!" Use strpos or stripos
  • Want to split up a string into an array by a known delimiter, e.g. a comma or pipe? Use explode
  • Want to trim whitespace or other known chars from beginning and/or end? Use trim or ltrim or rtrim
  • Want to replace or strip a known value from a string? Use str_replace

 

Not powerful enough
 
On the other end of that spectrum, regex sometimes isn't powerful enough for the job. The most common example of when regex falls short is that a lot of people turn to regex as a solution for trying to parse HTML/XML content. Sometimes regex may work out for you, depending on what you are trying to accomplish. But most of the time, regex is not what you should be using for HTML/XML manipulation. Regex has come a long way over the years, but at its core, regex is for parsing regular grammar, but html/xml is context-free grammar.

If you are looking to parse html/xml content, you should use a DOM parser. There are several libraries that are by default included with php, such as DOM or SimpleXML, as well as a lot of great 3rd party libraries, such as Simple HTML DOM or phpQuery.  Also, if you are using a php framework, there's a good chance it already has one or more of these (or its own) included and wrapped in your framework syntax.

Share this post


Link to post
Share on other sites

PHP kept crashing on preg_match* on large strings, why?

 

This is actually one I kept running into recently... There is a limit on the amount of recursions that PHP will do. You're likely running into this limit and should either up the limit or found a different way to go about your problem.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.