Jump to content

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

Link to comment
https://forums.phpfreaks.com/topic/273123-readme-php-regex-resources-faqs/
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.

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.

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.