Tutorial :Simple RegEx PHP


I have a string and I need to see if it contains the following "_archived".

I was using the following:


but get:

Warning: preg_match() [function.preg-match]: Unknown modifier '_' in /home/storrec/classes/class.main.php on line 70  

I am new to Regular Expressions so this is probably very easy.

Or should I be using something a lot simpler like

strstr($string, "_archived");  

Thanks in advance


strstr is enough in this case, but to solve your problem, you need to add delimiters to your regex. A delimiter is a special character that starts and ends the regex, like so:


The delimiter can be a lot of different characters, but usual choices are /, # and !. From the PHP manual:

Any character can be used for delimiter as long as it's not alphanumeric, backslash (), or the null byte. If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.

Read all about PHP regular expression syntax here.

You can see some examples of valid (and invalid) patterns in the PHP manual here.


strstr($string, "_archived");

Is going to be way easier for the problem you describe.

As is often quoted

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski


You just need some delimiters, e.g. enclose the pattern with /


Perl regexes let you use any delimiter, which is handy if your regex uses / a lot. I often find myself using braces for example:


Also, note that you don't need the (.*) bit as you aren't capturing the bit before "_archived", you're just testing to see if the string ends with it (that $ symbol on the end matches the end of the string)


If all you're looking for is if a string contains a string, then by all means use the simple version. But you can also simply do:

preg_match('/_archived/', $string);  




If you are only checking if the string exists in $string, strstr should be enough for you though.


strstr() or strpos() would be better for finding something like this, I would think. The error you're getting is due to not having any delimiters around your regular expression. Try using





... is probably the best way to implement this. You're right that a regex is overkill.


When you have a specific string you're looking for, using regular expressions in a bit of overkill. You'll be fine using one of PHP's standard string search functions here.

The strstr function will work, but conventional PHP Wisdom (read: myth, legend, superstition, the manual) says that using the strpos function will yield better performance. i.e., something like

if(strpos($string, '_archived') !== false) {  }  

You'll want to use the true inequality operator here, as strpos returns "0" when it finds a needle at the start of it's haystack. See the manual for more information on this.

As to your problem, PHP's regular expression engine expects you to enclose your regular expression string with a set of delimiters, which can be one of a number of different characters ( "/" or "|" or "(" or "{" or "#", or ...). The PHP engine thinks you want a regular expression of


with a set of pattern modifiers that are


So, in the future, when you use a regular expression, try something like

//equivilant  preg_match('/(.*)_archived$/i',$string);  preg_match('{(.*)_archived$}i',$string);  preg_match('#(.*)_archived$#i',$string);  

The "/", "#", and "{}" characters are your delimiters. They are not used in the match, they're used to tell the engine "anything in-between these characters is my reg ex. The "i" at the end is a pattern modifiers that says to be case insensitive. It's not necessary, but I include it here so you can see what a pattern modifier looks like, and to help you understand why you need delimiters in the first place.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »