Another preg_match() [function.preg-match]: Unknown modifier '{';-)

This is a discussion on Another preg_match() [function.preg-match]: Unknown modifier '{';-) within the PHP Language forums, part of the PHP Programming Forums category; function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('[A-Za-z0-...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-18-2008
JanDoggen
 
Posts: n/a
Default Another preg_match() [function.preg-match]: Unknown modifier '{';-)

function vldLicense($lic)
{
echo "called with lic: ". $lic . "<br>";
echo preg_match('[A-Za-z0-9]', $lic) . "<br>";

if (preg_match('[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-
z0-9]{4}', $lic) == 0) return false;
return true;
}

gives me:

called with lic: 64gd-kwey-t643-ytes
0
Warning: preg_match() [function.preg-match]: Unknown modifier '{'

Why the 0 in the simple match?
Why the unknown modifier in the complex match?

Thanks in advance
Jan
Reply With Quote
  #2 (permalink)  
Old 04-18-2008
Janwillem Borleffs
 
Posts: n/a
Default Re: Another preg_match() [function.preg-match]: Unknown modifier'{' ;-)

JanDoggen schreef:
> function vldLicense($lic)
> {
> echo "called with lic: ". $lic . "<br>";
> echo preg_match('[A-Za-z0-9]', $lic) . "<br>";
>
> if (preg_match('[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-
> z0-9]{4}', $lic) == 0) return false;
> return true;
> }
>
> gives me:
>
> called with lic: 64gd-kwey-t643-ytes
> 0
> Warning: preg_match() [function.preg-match]: Unknown modifier '{'
>
> Why the 0 in the simple match?
> Why the unknown modifier in the complex match?
>


In the first call, the square brackets are assumed to be modifiers. If
you would put something behind the character class (e.g.
'[A-Za-z0-9]a'), you will the same compliant pointing at the bracket.


JW
Reply With Quote
  #3 (permalink)  
Old 04-18-2008
Charles Calvert
 
Posts: n/a
Default Re: Another preg_match() [function.preg-match]: Unknown modifier '{' ;-)

On Fri, 18 Apr 2008 06:25:45 -0700 (PDT), JanDoggen
<JanDoggen@planet.nl> wrote in
<3f6b8dce-7783-43bd-a727-bfebcd333a38@k37g2000hsf.googlegroups.com>:

>function vldLicense($lic)
>{
>echo "called with lic: ". $lic . "<br>";


You can write this as:

echo "called with lic: $lic<br>";

php will replace $lic with the value of the variable. See the
difference between (") and (').

>echo preg_match('[A-Za-z0-9]', $lic) . "<br>";


echo preg_match('/[A-Za-z0-9]/', $lic) . "<br>";

Regexes are required to be bounded by a character. "/" is the usual
character used, but you can use others as well.

> if (preg_match('[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-
>z0-9]{4}', $lic) == 0) return false;


if (preg_match('/[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-
z0-9]{4}/', $lic) == 0) return false;

Again, put the regex inside of "/".

> return true;
>}
>
>gives me:
>
>called with lic: 64gd-kwey-t643-ytes
>0
>Warning: preg_match() [function.preg-match]: Unknown modifier '{'
>
>Why the 0 in the simple match?


Because preg_match was treating "[]" as the regex bounding characters
instead of the class boundary, so it read "[A-Za-z0-9]" as matching
the literal string "A-Za-z0-9".

>Why the unknown modifier in the complex match?


Same reason. It read the first occurrence of "[A-Za-z0-9]" as the
regex (match literal string "A-Za-z0-9") and then couldn't figure out
what to do with {4}.

--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:07 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0