RegEx 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
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

RegEx Help

Post by aliasxneo »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here is my code:

Code: Select all

$handle = fopen("players.txt", "r");
while (!feof($handle)) {
    $file .= fread($handle, 1024);
}
fclose($handle);

$parts = explode("[player]", $file);

$players = array();
$index = 0;
foreach($parts as $part)
{
    while (preg_match("/[(.*)](.*)[\/.*]/", $part, $match))
    {
        echo "test";
        $players[$index][$match[1]] = $match[2];
        $index++;
    }
}
Here is the contents of players.txt:

Code: Select all

[player]
    [name]john1[/name]
    [kills]124[/kills]
    [deaths]345[/deaths]
[/player]
[player]
    [name]bob1[/name]
    [kills]342[/kills]
    [deaths]54[/deaths]
[/player]
I don't understand why this is not working. The file splits fine, and I get each user's individual stats in the array $parts. The problem is that preg_match() is not working, it won't even recognize it as a valid pattern. Can someone help me fix this pattern or explain what's wrong with it? Thanks.

P.S If you havn't noticed I'm trying to parse the user stats into an array.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Jhorra
Forum Newbie
Posts: 24
Joined: Mon Aug 18, 2003 1:00 am

Post by Jhorra »

Where is $match given a value?
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post by aliasxneo »

In preg_match(). Look at php.net if you don't know what the function does.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I don't think you want to do a while(preg_match) because it won't loop through an array of results like you think, but anyway, isn't preg_match_all what you're after?
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post by aliasxneo »

Yes, that would be a better method:

Code: Select all

foreach($parts as $part)
{
    if(preg_match_all("/[(.*)](.*)[\/.*]/", $part, $matches))
    {
        foreach ($matches as $match)
        {
            $players[$index][$match[1]] = $match[2];
        }
    }
    $index++;
}
But that's not the point of the post. The point is that the RegEx is not matching and I don't know why. Looking at the file contents my code should work fine, but it won't match anything.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You're aware that square brackets need to be escaped to be interpreted literally?
Post Reply