Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
preg_match problem
Old
  (#1)
Guest
Guest
 
Posts: n/a
Default preg_match problem - 05-14-2007, 03:35 AM

Hi all,

I have a simple checking like

if (preg_match("/[\w\x2F]{6,}/",$a))

as I would like to allow all "word characters" as mentioned at
http://hu2.php.net/manual/hu/referen...ern.syntax.php
plus the '/' character, and at least 6 characters.

But it throws

Warning: preg_match(): Unknown modifier ']'

and returns false for "abc/de/ggg" which string should be okay.
If I omit the "\x2F", everything works fine but "/" characters are not
allowed. Anyone knows what I'm doing wrong? Maybe "/" characters can not
be put in patterns like this?

Thanks in advance,
Zoltán Németh
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: [PHP] preg_match problem
Old
  (#2)
Roman Neuhauser
Guest
 
Posts: n/a
Default Re: [PHP] preg_match problem - 05-14-2007, 03:35 AM

# EMAIL REMOVED / 2007-01-19 15:25:38 +0100:
> Hi all,
>
> I have a simple checking like
>
> if (preg_match("/[\w\x2F]{6,}/",$a))
>
> as I would like to allow all "word characters" as mentioned at
> http://hu2.php.net/manual/hu/referen...ern.syntax.php
> plus the '/' character, and at least 6 characters.
>
> But it throws
>
> Warning: preg_match(): Unknown modifier ']'
>
> and returns false for "abc/de/ggg" which string should be okay.
> If I omit the "\x2F", everything works fine but "/" characters are not
> allowed. Anyone knows what I'm doing wrong? Maybe "/" characters can not
> be put in patterns like this?


1. You're making your life harder with those double quotes. \x2F is
interpolated by PHP, before the PCRE library has a chance to see the
string.
2. Use a different delimiter and you'll be able to use slash characters
in the pattern freely.

preg_match('~[\w/]{6,}~', $a);

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match problem
Old
  (#3)
Guest
Guest
 
Posts: n/a
Default Re: [PHP] preg_match problem - 05-14-2007, 03:35 AM

On p, 2007-01-19 at 15:39 +0000, Roman Neuhauser wrote:
> # EMAIL REMOVED / 2007-01-19 15:25:38 +0100:
> > Hi all,
> >
> > I have a simple checking like
> >
> > if (preg_match("/[\w\x2F]{6,}/",$a))
> >
> > as I would like to allow all "word characters" as mentioned at
> > http://hu2.php.net/manual/hu/referen...ern.syntax.php
> > plus the '/' character, and at least 6 characters.
> >
> > But it throws
> >
> > Warning: preg_match(): Unknown modifier ']'
> >
> > and returns false for "abc/de/ggg" which string should be okay.
> > If I omit the "\x2F", everything works fine but "/" characters are not
> > allowed. Anyone knows what I'm doing wrong? Maybe "/" characters can not
> > be put in patterns like this?

>
> 1. You're making your life harder with those double quotes. \x2F is
> interpolated by PHP, before the PCRE library has a chance to see the
> string.
> 2. Use a different delimiter and you'll be able to use slash characters
> in the pattern freely.
>
> preg_match('~[\w/]{6,}~', $a);
>


Thank you very much, it's working fine now.

Zoltán Németh
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match problem
Old
  (#4)
Martin Alterisio
Guest
 
Posts: n/a
Default Re: [PHP] preg_match problem - 05-14-2007, 03:36 AM

Double slash to prevent PHP interpreting the slashes. Also using single
quotes would be a good idea:

if (preg_match('/[\\w\\x2F]{6,}/',$a))


2007/1/19, Németh Zoltán <EMAIL REMOVED>:
>
> Hi all,
>
> I have a simple checking like
>
> if (preg_match("/[\w\x2F]{6,}/",$a))
>
> as I would like to allow all "word characters" as mentioned at
> http://hu2.php.net/manual/hu/referen...ern.syntax.php
> plus the '/' character, and at least 6 characters.
>
> But it throws
>
> Warning: preg_match(): Unknown modifier ']'
>
> and returns false for "abc/de/ggg" which string should be okay.
> If I omit the "\x2F", everything works fine but "/" characters are not
> allowed. Anyone knows what I'm doing wrong? Maybe "/" characters can not
> be put in patterns like this?
>
> Thanks in advance,
> Zoltán Németh
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match problem
Old
  (#5)
Arpad Ray
Guest
 
Posts: n/a
Default Re: [PHP] preg_match problem - 05-14-2007, 03:36 AM

Martin Alterisio wrote:
> Double slash to prevent PHP interpreting the slashes. Also using single
> quotes would be a good idea:
>
> if (preg_match('/[\\w\\x2F]{6,}/',$a))
>


Just switching to single quotes would do the trick - you don't need to
escape anything but single quotes, and backslashes if they are the last
character.

Arpad
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match problem
Old
  (#6)
Martin Alterisio
Guest
 
Posts: n/a
Default Re: [PHP] preg_match problem - 05-14-2007, 03:36 AM

2007/1/20, Arpad Ray <EMAIL REMOVED>:
>
> Martin Alterisio wrote:
> > Double slash to prevent PHP interpreting the slashes. Also using single
> > quotes would be a good idea:
> >
> > if (preg_match('/[\\w\\x2F]{6,}/',$a))
> >

>
> Just switching to single quotes would do the trick - you don't need to
> escape anything but single quotes, and backslashes if they are the last
> character.
>
> Arpad
>
>

It's true, it works but I still believe is best to do both, since you may
forget to doubleslash when it's imperative for the expression to work, even
when using single quotes.

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top