Page 1 of 1

preg_match help

Posted: Wed Apr 14, 2010 9:41 am
by m2babaey
Hi
I cannot understand patterns in preg_match
The code below prints 80 in level="80"

Code: Select all

<?php
$data='<character battleGroup="Rampage" charUrl="r=Garona&cn=Bob" class="Hunter"
classId="3" level="80" name="Bob" >';

if(preg_match('/<character.{1,30} charUrl="r=Garona&cn=Bob".{1,50}level="(\d+)"/s', $data, $match))
{
    print $match[1];
}
?>
Could you please help me understand the code?
I ask:
1. Code starts with /<character => this means "<character" is required
2. ".{1,30}" means combining with a string that can be 1-30 characters long
3. ".{1,50}" the same as 2
4. what is "(\d+)"?
5. What is "/s"?
Thanks in advance

Re: preg_match help

Posted: Wed Apr 14, 2010 9:44 am
by roders
What are you trying to achieve?

Re: preg_match help

Posted: Wed Apr 14, 2010 9:53 am
by mikosiko

Re: preg_match help

Posted: Wed Apr 14, 2010 10:05 am
by AbraCadaver
1. yes
2. yes ( . is any character )
3. yes
4. \d is a digit and the ( ) captures that in matches
5. / is the ending delimiter since it was used as the starting delimiter, and s is a modifier making the . match newline characters