Philip Posted January 14, 2013 Share Posted January 14, 2013 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: I need help with some regex, how should I post? When shouldn't I use regex? PHP kept crashing on preg_match* on large strings, why? Some Resources Common REGEX patterns Cheat sheet ($2) regular-expressions.info Andy's Regex Tutorial Quote Link to comment https://forums.phpfreaks.com/topic/273123-readme-php-regex-resources-faqs/ Share on other sites More sharing options...
Philip Posted January 14, 2013 Author Share Posted January 14, 2013 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: Describe your problem Give lots of sample input data Give the matching expected data for the input data you gave (this is important!) If you already have some code (which you should!), show us the code and tell us what you think is going wrong with it 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. Quote Link to comment https://forums.phpfreaks.com/topic/273123-readme-php-regex-resources-faqs/#findComment-1405512 Share on other sites More sharing options...
Philip Posted January 14, 2013 Author Share Posted January 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273123-readme-php-regex-resources-faqs/#findComment-1405513 Share on other sites More sharing options...
Philip Posted January 14, 2013 Author Share Posted January 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273123-readme-php-regex-resources-faqs/#findComment-1405514 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.