Search code examples
phpvariablesflat-file

Create value with specific parts of a text file


Ok, I am working on a flatfile shoutbox, and I am trying to achieve a way to get the username from the flatfile and making it a variable so I can use it to make a call to the database to check if the user is admin so they can delete/ban users directly from the shoutbox.

This is an example line in the flatfile

<div><i><div class='date'>12/08/2012 18:56 pm&nbsp;&nbsp;</div></i> <div class='groupAdmin'><b>Admin</b></div><b><a href='javascript:;' onclick="shout_insert('@kira423- ')">kira423</a>:</b> hiya :D</div>

So I wanna take the username which is kira423 in this case and create a variable such as $shoutname and make it equal kira423

I have tried a google search and looked around on here, but was unable to find an answer, so I am hoping that I can get some insight on how to do this with a question of my own here.

Thanks,

Kira


Solution

  • You should use preg_match for those tasks like this:

    preg_match_all('|<div class=\'date\'>(?P<date>.*?)&nbsp;.*<a.*>(?P<user>.*)</a>|i', $data, $matches);
    var_dump($matches);
    

    Interating through all array elements:

    foreach ($matches['user'] as $key => $user) {
        var_dump($user);
    }