preg_match help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

preg_match help

Post 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
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: preg_match help

Post by roders »

What are you trying to achieve?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: preg_match help

Post by mikosiko »

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: preg_match help

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply