tags:

views:

121

answers:

8
+1  Q: 

Simple RegEx PHP

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

I was using the following:

preg_match('(.*)_archived$',$string);

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

+1  A: 

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

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

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

preg_match('{_archived$}',$string);

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)

Paul Dixon
A: 

Try:

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

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

ylebre
A: 

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

"/(.*)_archived$/"

or

"#(.*)_archived$#".
Jeremy Privett
+5  A: 

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:

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

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.

PatrikAkerstrand
thanks I will use the strstr as reg ex would be over kill, you know what developers are like, they find something new and just want to play!
Lizard
Oh absolutely. Regex certainly has some very nice areas where they can be used. In Zend Framework they are often used to create "magic methods" that map to other methods, but make the code easier to read. It basically works by overriding the PHP __call($functionName, $args)-function, and use regex to match the relevant parts of the functionName and map them to a real methods: i.e. findXByY() => find('X', 'Y')
PatrikAkerstrand
+7  A: 

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

saturdayplace
+1 for the quote
schnaader
+1  A: 

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);
Lee
A: 

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

A: 

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

_archived$

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.

Alan Storm