Forum Moderators: phranque

Message Too Old, No Replies

Regex syntax problems

Possibly PHP specific?

         

TGecho

1:31 am on Oct 2, 2003 (gmt 0)

10+ Year Member



I'm trying to use a regular expression I hacked from regxlib.com. It worked fine in their test box, but when I insert it into my PHP script, it returns an error.

My code:
if(preg_match("^[a-zA-Z]\w{0,25}$", $str) == 'false')

Warning: No ending delimiter '^' found in c:\program files\nusphere\apache\nsdocs\cms\modules\cm_pm.php on line 64

This is supposed to make sure the first character is a letter, the rest are alphanumeric, and it isn't longer than 24 characters.

Is there some PHP specific thing going on here?

claus

1:39 am on Oct 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm just guessing as i don't really know php. Perhaps i'm totally wrong.

The ^ delimiter marks the beginning of the string that you check. Line 64 is not the beginning of the string, so it does not match. Try omitting it and see if that solves the problem.

/claus

MonkeeSage

1:52 am on Oct 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure but I think you want single quotes (don't interpret the string) and the regexp delimiters...try this...

preg_match('/^[a-zA-Z]\w{0,25}$/', $str)

Jordan

jatar_k

2:05 am on Oct 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice MonkeeSage, the double quotes with the dollar sign between will make a mess

TGecho

2:10 am on Oct 2, 2003 (gmt 0)

10+ Year Member



Thanks! That fixed the parsing error, but then I wasn't getting proper matches (or so I thought).

I was actually testing for 'true', I'd been experimenting with the code I posted to try and debug it. Anyway, I tried testing for '1' and it now works perfectly.

So, are you sure this isn't black magic? ;)