0

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

kira423
  • 325
  • 1
  • 5
  • 26

2 Answers2

1

I think you should just parse each line in the flatfile as HTML (there are simple HTML tags used), just like described in PHP Parse HTML code (or type "php parse HTML" in google). Then you may access the username (kira123) from an array or whatever.

PS HTML is not the best way you can store messages to display. Even CSV seems to be better - it'd be "kira123;date;some text" - it's easier to read and to access each part. When displaying, use the standar decorator pattern.

Community
  • 1
  • 1
ducin
  • 25,621
  • 41
  • 157
  • 256
1

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);
}
Benjamin Paap
  • 2,744
  • 2
  • 21
  • 33
  • I took the line from your example above. Make sure it is exactly one line. – Benjamin Paap Dec 08 '12 at 18:52
  • Ok so if there are multiple lines with different usernames then this wont work? – kira423 Dec 08 '12 at 18:55
  • regular expressions only work for single lines. You can add the `g` modifier or use `preg_match_all` if that helps but I would suggest reading your flat file line by line. – Benjamin Paap Dec 08 '12 at 18:56
  • I am not really good at this type of thing as I don't do files that much, but how can I use your method to create the variable I need to make a database query call if its only going to return one username instead of all of them, preg_match_all wont work because it returns them all in 1 array so I can single them out with their array key – kira423 Dec 08 '12 at 19:01
  • You should be able to iterate through all usernames by a simple foreach? I don't understand where the problem might be for that. Eventually I misunderstood your question. – Benjamin Paap Dec 08 '12 at 19:03
  • No you understood the question, I just don't understand what you mean by reading it line by line, I don't normally use text files for anything, so I don't really know how to work with them other than opening and adding to them – kira423 Dec 08 '12 at 19:07
  • Updates the answer to show you how to iterate through the users. This has nothing to do with "using textfiles". This is pure simple php which you should be able to write yourself. – Benjamin Paap Dec 08 '12 at 19:12